7bb208cf72555d06cf676dc9354e9a4b726c3435
[mesa.git] / src / mesa / tnl / t_array_import.c
1 /* $Id: t_array_import.c,v 1.13 2001/04/26 14:53:48 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 "context.h"
32 #include "macros.h"
33 #include "mem.h"
34 #include "mmath.h"
35 #include "state.h"
36 #include "mtypes.h"
37
38 #include "array_cache/acache.h"
39 #include "math/m_translate.h"
40
41 #include "t_array_import.h"
42 #include "t_context.h"
43
44
45 static void _tnl_import_vertex( GLcontext *ctx,
46 GLboolean writeable,
47 GLboolean stride )
48 {
49 struct gl_client_array *tmp;
50 GLboolean is_writeable = 0;
51 struct vertex_arrays *inputs = &TNL_CONTEXT(ctx)->array_inputs;
52
53 tmp = _ac_import_vertex(ctx,
54 GL_FLOAT,
55 stride ? 4*sizeof(GLfloat) : 0,
56 0,
57 writeable,
58 &is_writeable);
59
60 inputs->Obj.data = (GLfloat (*)[4]) tmp->Ptr;
61 inputs->Obj.start = (GLfloat *) tmp->Ptr;
62 inputs->Obj.stride = tmp->StrideB;
63 inputs->Obj.size = tmp->Size;
64 inputs->Obj.flags &= ~(VEC_BAD_STRIDE|VEC_NOT_WRITEABLE);
65 if (inputs->Obj.stride != 4*sizeof(GLfloat))
66 inputs->Obj.flags |= VEC_BAD_STRIDE;
67 if (!is_writeable)
68 inputs->Obj.flags |= VEC_NOT_WRITEABLE;
69 }
70
71 static void _tnl_import_normal( GLcontext *ctx,
72 GLboolean writeable,
73 GLboolean stride )
74 {
75 struct gl_client_array *tmp;
76 GLboolean is_writeable = 0;
77 struct vertex_arrays *inputs = &TNL_CONTEXT(ctx)->array_inputs;
78
79 tmp = _ac_import_normal(ctx, GL_FLOAT,
80 stride ? 3*sizeof(GLfloat) : 0, writeable,
81 &is_writeable);
82
83 inputs->Normal.data = (GLfloat (*)[3]) tmp->Ptr;
84 inputs->Normal.start = (GLfloat *) tmp->Ptr;
85 inputs->Normal.stride = tmp->StrideB;
86 inputs->Normal.flags &= ~(VEC_BAD_STRIDE|VEC_NOT_WRITEABLE);
87 if (inputs->Normal.stride != 3*sizeof(GLfloat))
88 inputs->Normal.flags |= VEC_BAD_STRIDE;
89 if (!is_writeable)
90 inputs->Normal.flags |= VEC_NOT_WRITEABLE;
91 }
92
93
94 static void _tnl_import_color( GLcontext *ctx,
95 GLboolean writeable,
96 GLboolean stride )
97 {
98 struct gl_client_array *tmp;
99 GLboolean is_writeable = 0;
100 struct vertex_arrays *inputs = &TNL_CONTEXT(ctx)->array_inputs;
101
102 /* fprintf(stderr, "%s\n", __FUNCTION__); */
103
104 tmp = _ac_import_color(ctx,
105 GL_UNSIGNED_BYTE,
106 stride ? 4*sizeof(GLubyte) : 0,
107 4,
108 writeable,
109 &is_writeable);
110
111 inputs->Color.data = (GLchan (*)[4]) tmp->Ptr;
112 inputs->Color.start = (GLchan *) tmp->Ptr;
113 inputs->Color.stride = tmp->StrideB;
114 inputs->Color.flags &= ~(VEC_BAD_STRIDE|VEC_NOT_WRITEABLE);
115 if (inputs->Color.stride != 4*sizeof(GLchan))
116 inputs->Color.flags |= VEC_BAD_STRIDE;
117 if (!is_writeable)
118 inputs->Color.flags |= VEC_NOT_WRITEABLE;
119 }
120
121
122 static void _tnl_import_secondarycolor( GLcontext *ctx,
123 GLboolean writeable,
124 GLboolean stride )
125 {
126 struct gl_client_array *tmp;
127 GLboolean is_writeable = 0;
128 struct vertex_arrays *inputs = &TNL_CONTEXT(ctx)->array_inputs;
129
130 tmp = _ac_import_secondarycolor(ctx, GL_UNSIGNED_BYTE,
131 stride ? 4*sizeof(GLubyte) : 0,
132 4,
133 writeable,
134 &is_writeable);
135
136 inputs->SecondaryColor.data = (GLchan (*)[4]) tmp->Ptr;
137 inputs->SecondaryColor.start = (GLchan *) tmp->Ptr;
138 inputs->SecondaryColor.stride = tmp->StrideB;
139 inputs->SecondaryColor.flags &= ~(VEC_BAD_STRIDE|VEC_NOT_WRITEABLE);
140 if (inputs->SecondaryColor.stride != 4*sizeof(GLubyte))
141 inputs->SecondaryColor.flags |= VEC_BAD_STRIDE;
142 if (!is_writeable)
143 inputs->SecondaryColor.flags |= VEC_NOT_WRITEABLE;
144 }
145
146 static void _tnl_import_fogcoord( GLcontext *ctx,
147 GLboolean writeable,
148 GLboolean stride )
149 {
150 struct vertex_arrays *inputs = &TNL_CONTEXT(ctx)->array_inputs;
151 struct gl_client_array *tmp;
152 GLboolean is_writeable = 0;
153
154 tmp = _ac_import_fogcoord(ctx, GL_FLOAT,
155 stride ? sizeof(GLfloat) : 0, writeable,
156 &is_writeable);
157
158 inputs->FogCoord.data = (GLfloat *) tmp->Ptr;
159 inputs->FogCoord.start = (GLfloat *) tmp->Ptr;
160 inputs->FogCoord.stride = tmp->StrideB;
161 inputs->FogCoord.flags &= ~(VEC_BAD_STRIDE|VEC_NOT_WRITEABLE);
162 if (inputs->FogCoord.stride != sizeof(GLfloat))
163 inputs->FogCoord.flags |= VEC_BAD_STRIDE;
164 if (!is_writeable)
165 inputs->FogCoord.flags |= VEC_NOT_WRITEABLE;
166 }
167
168 static void _tnl_import_index( GLcontext *ctx,
169 GLboolean writeable,
170 GLboolean stride )
171 {
172 struct vertex_arrays *inputs = &TNL_CONTEXT(ctx)->array_inputs;
173 struct gl_client_array *tmp;
174 GLboolean is_writeable = 0;
175
176 tmp = _ac_import_index(ctx, GL_UNSIGNED_INT,
177 stride ? sizeof(GLuint) : 0, writeable,
178 &is_writeable);
179
180 inputs->Index.data = (GLuint *) tmp->Ptr;
181 inputs->Index.start = (GLuint *) tmp->Ptr;
182 inputs->Index.stride = tmp->StrideB;
183 inputs->Index.flags &= ~(VEC_BAD_STRIDE|VEC_NOT_WRITEABLE);
184 if (inputs->Index.stride != sizeof(GLuint))
185 inputs->Index.flags |= VEC_BAD_STRIDE;
186 if (!is_writeable)
187 inputs->Index.flags |= VEC_NOT_WRITEABLE;
188 }
189
190
191 static void _tnl_import_texcoord( GLcontext *ctx,
192 GLuint i,
193 GLboolean writeable,
194 GLboolean stride )
195 {
196 struct vertex_arrays *inputs = &TNL_CONTEXT(ctx)->array_inputs;
197 struct gl_client_array *tmp;
198 GLboolean is_writeable = 0;
199
200 tmp = _ac_import_texcoord(ctx, i, GL_FLOAT,
201 stride ? 4*sizeof(GLfloat) : 0,
202 0,
203 writeable,
204 &is_writeable);
205
206 inputs->TexCoord[i].data = (GLfloat (*)[4]) tmp->Ptr;
207 inputs->TexCoord[i].start = (GLfloat *) tmp->Ptr;
208 inputs->TexCoord[i].stride = tmp->StrideB;
209 inputs->TexCoord[i].size = tmp->Size;
210 inputs->TexCoord[i].flags &= ~(VEC_BAD_STRIDE|VEC_NOT_WRITEABLE);
211 if (inputs->TexCoord[i].stride != 4*sizeof(GLfloat))
212 inputs->TexCoord[i].flags |= VEC_BAD_STRIDE;
213 if (!is_writeable)
214 inputs->TexCoord[i].flags |= VEC_NOT_WRITEABLE;
215 }
216
217
218 static void _tnl_import_edgeflag( GLcontext *ctx,
219 GLboolean writeable,
220 GLboolean stride )
221 {
222 struct vertex_arrays *inputs = &TNL_CONTEXT(ctx)->array_inputs;
223 struct gl_client_array *tmp;
224 GLboolean is_writeable = 0;
225
226 tmp = _ac_import_edgeflag(ctx, GL_UNSIGNED_BYTE,
227 stride ? sizeof(GLubyte) : 0,
228 0,
229 &is_writeable);
230
231 inputs->EdgeFlag.data = (GLubyte *) tmp->Ptr;
232 inputs->EdgeFlag.start = (GLubyte *) tmp->Ptr;
233 inputs->EdgeFlag.stride = tmp->StrideB;
234 inputs->EdgeFlag.flags &= ~(VEC_BAD_STRIDE|VEC_NOT_WRITEABLE);
235 if (inputs->EdgeFlag.stride != sizeof(GLubyte))
236 inputs->EdgeFlag.flags |= VEC_BAD_STRIDE;
237 if (!is_writeable)
238 inputs->EdgeFlag.flags |= VEC_NOT_WRITEABLE;
239 }
240
241
242
243 /* Callback for VB stages that need to improve the quality of arrays
244 * bound to the VB. This is only necessary for client arrays which
245 * have not been transformed at any point in the pipeline.
246 */
247 static void _tnl_upgrade_client_data( GLcontext *ctx,
248 GLuint required,
249 GLuint flags )
250 {
251 GLuint i;
252 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
253 GLboolean writeable = (flags & VEC_NOT_WRITEABLE) != 0;
254 GLboolean stride = (flags & VEC_BAD_STRIDE) != 0;
255 struct vertex_arrays *inputs = &TNL_CONTEXT(ctx)->array_inputs;
256 (void) inputs;
257
258 if ((required & VERT_CLIP) && VB->ClipPtr == VB->ObjPtr)
259 required |= VERT_OBJ;
260
261 /* _tnl_print_vert_flags("_tnl_upgrade_client_data", required); */
262
263 if ((required & VERT_OBJ) && (VB->ObjPtr->flags & flags)) {
264 ASSERT(VB->ObjPtr == &inputs->Obj);
265 _tnl_import_vertex( ctx, writeable, stride );
266 VB->importable_data &= ~(VERT_OBJ|VERT_CLIP);
267 }
268
269 if ((required & VERT_NORM) && (VB->NormalPtr->flags & flags)) {
270 ASSERT(VB->NormalPtr == &inputs->Normal);
271 _tnl_import_normal( ctx, writeable, stride );
272 VB->importable_data &= ~VERT_NORM;
273 }
274
275 if ((required & VERT_RGBA) && (VB->ColorPtr[0]->flags & flags)) {
276 ASSERT(VB->ColorPtr[0] == &inputs->Color);
277 _tnl_import_color( ctx, writeable, stride );
278 VB->importable_data &= ~VERT_RGBA;
279 }
280
281 if ((required & VERT_SPEC_RGB) && (VB->SecondaryColorPtr[0]->flags&flags)) {
282 ASSERT(VB->SecondaryColorPtr[0] == &inputs->SecondaryColor);
283 _tnl_import_secondarycolor( ctx, writeable, stride );
284 VB->importable_data &= ~VERT_SPEC_RGB;
285 }
286
287 if ((required & VERT_FOG_COORD) && (VB->FogCoordPtr->flags & flags)) {
288 ASSERT(VB->FogCoordPtr == &inputs->FogCoord);
289 _tnl_import_fogcoord( ctx, writeable, stride );
290 VB->importable_data &= ~VERT_FOG_COORD;
291 }
292
293 if ((required & VERT_INDEX) && (VB->IndexPtr[0]->flags & flags)) {
294 ASSERT(VB->IndexPtr[0] == &inputs->Index);
295 _tnl_import_index( ctx, writeable, stride );
296 VB->importable_data &= ~VERT_INDEX;
297 }
298
299 if (required & VERT_TEX_ANY)
300 for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++)
301 if ((required & VERT_TEX(i)) && (VB->TexCoordPtr[i]->flags & flags)) {
302 ASSERT(VB->TexCoordPtr[i] == &inputs->TexCoord[i]);
303 _tnl_import_texcoord( ctx, i, writeable, stride );
304 VB->importable_data &= ~VERT_TEX(i);
305 }
306
307 }
308
309
310
311
312
313 void _tnl_vb_bind_arrays( GLcontext *ctx, GLint start, GLsizei count )
314 {
315 TNLcontext *tnl = TNL_CONTEXT(ctx);
316 struct vertex_buffer *VB = &tnl->vb;
317 GLuint inputs = tnl->pipeline.inputs;
318 GLuint imports;
319 struct vertex_arrays *tmp = &tnl->array_inputs;
320
321 /* fprintf(stderr, "_tnl_vb_bind_arrays %d..%d // %d..%d\n", */
322 /* start, count, ctx->Array.LockFirst, ctx->Array.LockCount); */
323
324 if (ctx->Array.LockCount) {
325 ASSERT(start == (GLint) ctx->Array.LockFirst);
326 ASSERT(count == (GLint) ctx->Array.LockCount);
327 }
328
329 imports = tnl->pipeline.inputs;
330
331 _ac_import_range( ctx, start, count );
332
333 VB->Count = count - start;
334 VB->FirstClipped = VB->Count;
335
336 VB->Elts = 0;
337 VB->MaterialMask = 0;
338 VB->Material = 0;
339 VB->Flag = 0;
340
341 /* _tnl_print_vert_flags("_tnl_vb_bind_arrays: inputs", inputs); */
342 /* _tnl_print_vert_flags("_tnl_vb_bind_arrays: imports", imports); */
343 /* _tnl_print_vert_flags("_tnl_vb_bind_arrays: _Enabled", ctx->Array._Enabled); */
344
345 if (inputs & VERT_OBJ) {
346 if (imports & VERT_OBJ) {
347 _tnl_import_vertex( ctx, 0, 0 );
348 tmp->Obj.count = VB->Count;
349 }
350 VB->ObjPtr = &tmp->Obj;
351 }
352
353 if (inputs & VERT_NORM) {
354 if (imports & VERT_NORM) {
355 _tnl_import_normal( ctx, 0, 0 );
356 tmp->Normal.count = VB->Count;
357 }
358 VB->NormalPtr = &tmp->Normal;
359 }
360
361 if (inputs & VERT_RGBA) {
362 if (imports & VERT_RGBA) {
363 _tnl_import_color( ctx, 0, 0 );
364 tmp->Color.count = VB->Count;
365 }
366 VB->ColorPtr[0] = &tmp->Color;
367 VB->ColorPtr[1] = 0;
368 }
369
370 if (inputs & VERT_INDEX) {
371 if (imports & VERT_INDEX) {
372 _tnl_import_index( ctx, 0, 0 );
373 tmp->Index.count = VB->Count;
374 }
375 VB->IndexPtr[0] = &tmp->Index;
376 VB->IndexPtr[1] = 0;
377 }
378
379
380 if (inputs & VERT_FOG_COORD) {
381 if (imports & VERT_FOG_COORD) {
382 _tnl_import_fogcoord( ctx, 0, 0 );
383 tmp->FogCoord.count = VB->Count;
384 }
385 VB->FogCoordPtr = &tmp->FogCoord;
386 }
387
388 if (inputs & VERT_EDGE) {
389 _tnl_import_edgeflag( ctx, GL_TRUE, sizeof(GLboolean) );
390 VB->EdgeFlag = (GLboolean *) tmp->EdgeFlag.data;
391 }
392
393 if (inputs & VERT_SPEC_RGB) {
394 if (imports & VERT_SPEC_RGB) {
395 _tnl_import_secondarycolor( ctx, 0, 0 );
396 tmp->SecondaryColor.count = VB->Count;
397 }
398 VB->SecondaryColorPtr[0] = &tmp->SecondaryColor;
399 VB->SecondaryColorPtr[1] = 0;
400 }
401
402 if (inputs & VERT_TEX_ANY) {
403 GLuint i;
404 for (i = 0; i < ctx->Const.MaxTextureUnits ; i++)
405 if (inputs & VERT_TEX(i)) {
406 if (imports & VERT_TEX(i)) {
407 _tnl_import_texcoord( ctx, i, 0, 0 );
408 tmp->TexCoord[i].count = VB->Count;
409 }
410 VB->TexCoordPtr[i] = &tmp->TexCoord[i];
411 }
412 }
413
414 VB->Primitive = tnl->tmp_primitive;
415 VB->PrimitiveLength = tnl->tmp_primitive_length;
416 VB->import_data = _tnl_upgrade_client_data;
417 VB->importable_data = imports & VERT_FIXUP;
418 /* _tnl_print_vert_flags("_tnl_vb_bind_arrays: importable", VB->importable_data); */
419
420 }
421
422
423
424
425 /* Function to fill an immediate struct with the effects of
426 * consecutive calls to ArrayElement with consecutive indices.
427 */
428 void _tnl_fill_immediate_drawarrays( GLcontext *ctx, struct immediate *IM,
429 GLuint start, GLuint count )
430 {
431 TNLcontext *tnl = TNL_CONTEXT(ctx);
432 GLuint required = ctx->Array._Enabled;
433 GLuint n = count - start;
434 GLuint i;
435
436 if (!ctx->CompileFlag)
437 required &= tnl->pipeline.inputs;
438
439 if (MESA_VERBOSE&VERBOSE_IMMEDIATE)
440 fprintf(stderr, "exec_full_array_elements %d .. %d\n", start, count);
441
442 _math_trans_4f( IM->Obj + IM->Start,
443 ctx->Array.Vertex.Ptr,
444 ctx->Array.Vertex.StrideB,
445 ctx->Array.Vertex.Type,
446 ctx->Array.Vertex.Size,
447 start, n );
448
449 if (ctx->Array.Vertex.Size == 4)
450 required |= VERT_OBJ_234;
451 else if (ctx->Array.Vertex.Size == 3)
452 required |= VERT_OBJ_23;
453
454
455 if (required & VERT_NORM) {
456 _math_trans_3f( IM->Normal + IM->Start,
457 ctx->Array.Normal.Ptr,
458 ctx->Array.Normal.StrideB,
459 ctx->Array.Normal.Type,
460 start, n );
461 }
462
463 if (required & VERT_EDGE) {
464 _math_trans_1ub( IM->EdgeFlag + IM->Start,
465 ctx->Array.EdgeFlag.Ptr,
466 ctx->Array.EdgeFlag.StrideB,
467 ctx->Array.EdgeFlag.Type,
468 start, n );
469 }
470
471 if (required & VERT_RGBA) {
472 _math_trans_4chan( IM->Color + IM->Start,
473 ctx->Array.Color.Ptr,
474 ctx->Array.Color.StrideB,
475 ctx->Array.Color.Type,
476 ctx->Array.Color.Size,
477 start, n );
478 }
479
480 if (required & VERT_SPEC_RGB) {
481 _math_trans_4chan( IM->SecondaryColor + IM->Start,
482 ctx->Array.SecondaryColor.Ptr,
483 ctx->Array.SecondaryColor.StrideB,
484 ctx->Array.SecondaryColor.Type,
485 ctx->Array.SecondaryColor.Size,
486 start, n );
487 }
488
489 if (required & VERT_FOG_COORD) {
490 _math_trans_1f( IM->FogCoord + IM->Start,
491 ctx->Array.FogCoord.Ptr,
492 ctx->Array.FogCoord.StrideB,
493 ctx->Array.FogCoord.Type,
494 start, n );
495 }
496
497 if (required & VERT_INDEX) {
498 _math_trans_1ui( IM->Index + IM->Start,
499 ctx->Array.Index.Ptr,
500 ctx->Array.Index.StrideB,
501 ctx->Array.Index.Type,
502 start, n );
503 }
504
505 if (required & VERT_TEX_ANY) {
506 for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++) {
507 if (required & VERT_TEX(i)) {
508 _math_trans_4f( IM->TexCoord[i] + IM->Start,
509 ctx->Array.TexCoord[i].Ptr,
510 ctx->Array.TexCoord[i].StrideB,
511 ctx->Array.TexCoord[i].Type,
512 ctx->Array.TexCoord[i].Size,
513 start, n );
514
515 if (ctx->Array.TexCoord[i].Size == 4)
516 IM->TexSize |= TEX_SIZE_4(i);
517 else if (ctx->Array.TexCoord[i].Size == 3)
518 IM->TexSize |= TEX_SIZE_3(i);
519 }
520 }
521 }
522
523 IM->Count = IM->Start + n;
524 IM->Flag[IM->Start] |= required;
525 for (i = IM->Start+1 ; i < IM->Count ; i++)
526 IM->Flag[i] = required;
527 }