Add render stage for unclipped vb's to fx driver.
[mesa.git] / src / mesa / tnl / t_array_import.c
1 /* $Id: t_array_import.c,v 1.3 2000/12/28 22:11:05 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 "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 = 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 = 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 tmp = _ac_import_color(ctx,
103 GL_UNSIGNED_BYTE,
104 stride ? 4*sizeof(GLubyte) : 0,
105 4,
106 writeable,
107 &is_writeable);
108
109 inputs->Color.data = tmp->Ptr;
110 inputs->Color.start = (GLubyte *)tmp->Ptr;
111 inputs->Color.stride = tmp->StrideB;
112 inputs->Color.flags &= ~(VEC_BAD_STRIDE|VEC_NOT_WRITEABLE);
113 if (inputs->Color.stride != 4*sizeof(GLubyte))
114 inputs->Color.flags |= VEC_BAD_STRIDE;
115 if (!is_writeable)
116 inputs->Color.flags |= VEC_NOT_WRITEABLE;
117 }
118
119
120 static void _tnl_import_secondarycolor( GLcontext *ctx,
121 GLboolean writeable,
122 GLboolean stride )
123 {
124 struct gl_client_array *tmp;
125 GLboolean is_writeable = 0;
126 struct vertex_arrays *inputs = &TNL_CONTEXT(ctx)->array_inputs;
127
128 tmp = _ac_import_secondarycolor(ctx, GL_UNSIGNED_BYTE,
129 stride ? 4*sizeof(GLubyte) : 0,
130 4,
131 writeable,
132 &is_writeable);
133
134 inputs->SecondaryColor.data = tmp->Ptr;
135 inputs->SecondaryColor.start = (GLubyte *)tmp->Ptr;
136 inputs->SecondaryColor.stride = tmp->StrideB;
137 inputs->SecondaryColor.flags &= ~(VEC_BAD_STRIDE|VEC_NOT_WRITEABLE);
138 if (inputs->SecondaryColor.stride != 4*sizeof(GLubyte))
139 inputs->SecondaryColor.flags |= VEC_BAD_STRIDE;
140 if (!is_writeable)
141 inputs->SecondaryColor.flags |= VEC_NOT_WRITEABLE;
142 }
143
144 static void _tnl_import_fogcoord( GLcontext *ctx,
145 GLboolean writeable,
146 GLboolean stride )
147 {
148 struct vertex_arrays *inputs = &TNL_CONTEXT(ctx)->array_inputs;
149 struct gl_client_array *tmp;
150 GLboolean is_writeable = 0;
151
152 tmp = _ac_import_fogcoord(ctx, GL_FLOAT,
153 stride ? sizeof(GLfloat) : 0, writeable,
154 &is_writeable);
155
156 inputs->FogCoord.data = tmp->Ptr;
157 inputs->FogCoord.start = (GLfloat *)tmp->Ptr;
158 inputs->FogCoord.stride = tmp->StrideB;
159 inputs->FogCoord.flags &= ~(VEC_BAD_STRIDE|VEC_NOT_WRITEABLE);
160 if (inputs->FogCoord.stride != sizeof(GLfloat))
161 inputs->FogCoord.flags |= VEC_BAD_STRIDE;
162 if (!is_writeable)
163 inputs->FogCoord.flags |= VEC_NOT_WRITEABLE;
164 }
165
166 static void _tnl_import_index( GLcontext *ctx,
167 GLboolean writeable,
168 GLboolean stride )
169 {
170 struct vertex_arrays *inputs = &TNL_CONTEXT(ctx)->array_inputs;
171 struct gl_client_array *tmp;
172 GLboolean is_writeable = 0;
173
174 tmp = _ac_import_index(ctx, GL_UNSIGNED_INT,
175 stride ? sizeof(GLuint) : 0, writeable,
176 &is_writeable);
177
178 inputs->Index.data = tmp->Ptr;
179 inputs->Index.start = (GLuint *)tmp->Ptr;
180 inputs->Index.stride = tmp->StrideB;
181 inputs->Index.flags &= ~(VEC_BAD_STRIDE|VEC_NOT_WRITEABLE);
182 if (inputs->Index.stride != sizeof(GLuint))
183 inputs->Index.flags |= VEC_BAD_STRIDE;
184 if (!is_writeable)
185 inputs->Index.flags |= VEC_NOT_WRITEABLE;
186 }
187
188
189 static void _tnl_import_texcoord( GLcontext *ctx,
190 GLuint i,
191 GLboolean writeable,
192 GLboolean stride )
193 {
194 struct vertex_arrays *inputs = &TNL_CONTEXT(ctx)->array_inputs;
195 struct gl_client_array *tmp;
196 GLboolean is_writeable = 0;
197
198 /* fprintf(stderr, "%s %d before wr %d ptr %p\n", __FUNCTION__, i, writeable, */
199 /* inputs->TexCoord[i].data); */
200
201 tmp = _ac_import_texcoord(ctx, i, GL_FLOAT,
202 stride ? 4*sizeof(GLfloat) : 0,
203 0,
204 writeable,
205 &is_writeable);
206
207 inputs->TexCoord[i].data = tmp->Ptr;
208 inputs->TexCoord[i].start = (GLfloat *)tmp->Ptr;
209 inputs->TexCoord[i].stride = tmp->StrideB;
210 inputs->TexCoord[i].size = tmp->Size;
211 inputs->TexCoord[i].flags &= ~(VEC_BAD_STRIDE|VEC_NOT_WRITEABLE);
212 if (inputs->TexCoord[i].stride != 4*sizeof(GLfloat))
213 inputs->TexCoord[i].flags |= VEC_BAD_STRIDE;
214 if (!is_writeable)
215 inputs->TexCoord[i].flags |= VEC_NOT_WRITEABLE;
216
217 /* fprintf(stderr, "%s %d before wr %d ptr %p\n", __FUNCTION__, i, is_writeable, */
218 /* inputs->TexCoord[i].data); */
219 }
220
221
222 static void _tnl_import_edgeflag( GLcontext *ctx,
223 GLboolean writeable,
224 GLboolean stride )
225 {
226 struct vertex_arrays *inputs = &TNL_CONTEXT(ctx)->array_inputs;
227 struct gl_client_array *tmp;
228 GLboolean is_writeable = 0;
229
230 tmp = _ac_import_edgeflag(ctx, GL_UNSIGNED_BYTE,
231 stride ? sizeof(GLubyte) : 0,
232 0,
233 &is_writeable);
234
235 inputs->EdgeFlag.data = tmp->Ptr;
236 inputs->EdgeFlag.start = (GLubyte *)tmp->Ptr;
237 inputs->EdgeFlag.stride = tmp->StrideB;
238 inputs->EdgeFlag.flags &= ~(VEC_BAD_STRIDE|VEC_NOT_WRITEABLE);
239 if (inputs->EdgeFlag.stride != sizeof(GLubyte))
240 inputs->EdgeFlag.flags |= VEC_BAD_STRIDE;
241 if (!is_writeable)
242 inputs->EdgeFlag.flags |= VEC_NOT_WRITEABLE;
243 }
244
245
246
247 /* Callback for VB stages that need to improve the quality of arrays
248 * bound to the VB. This is only necessary for client arrays which
249 * have not been transformed at any point in the pipeline.
250 */
251 static void _tnl_upgrade_client_data( GLcontext *ctx,
252 GLuint required,
253 GLuint flags )
254 {
255 GLuint i;
256 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
257 GLboolean writeable = (flags & VEC_NOT_WRITEABLE) != 0;
258 GLboolean stride = (flags & VEC_BAD_STRIDE) != 0;
259 struct vertex_arrays *inputs = &TNL_CONTEXT(ctx)->array_inputs;
260 (void) inputs;
261
262 if ((required & VERT_CLIP) && VB->ClipPtr == VB->ObjPtr)
263 required |= VERT_OBJ;
264
265 /* _tnl_print_vert_flags("_tnl_upgrade_client_data", required); */
266
267 if ((required & VERT_OBJ) && (VB->ObjPtr->flags & flags)) {
268 ASSERT(VB->ObjPtr == &inputs->Obj);
269 _tnl_import_vertex( ctx, writeable, stride );
270 }
271
272 if ((required & VERT_NORM) && (VB->NormalPtr->flags & flags)) {
273 ASSERT(VB->NormalPtr == &inputs->Normal);
274 _tnl_import_normal( ctx, writeable, stride );
275 }
276
277 if ((required & VERT_RGBA) && (VB->ColorPtr[0]->flags & flags)) {
278 ASSERT(VB->ColorPtr[0] == &inputs->Color);
279 _tnl_import_color( ctx, writeable, stride );
280 }
281
282 if ((required & VERT_SPEC_RGB) && (VB->SecondaryColorPtr[0]->flags&flags)) {
283 ASSERT(VB->SecondaryColorPtr[0] == &inputs->SecondaryColor);
284 _tnl_import_secondarycolor( ctx, writeable, stride );
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 }
291
292 if ((required & VERT_INDEX) && (VB->IndexPtr[0]->flags & flags)) {
293 ASSERT(VB->IndexPtr[0] == &inputs->Index);
294 _tnl_import_index( ctx, writeable, stride );
295 }
296
297 if (required & VERT_TEX_ANY)
298 for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++)
299 if ((required & VERT_TEX(i)) && (VB->TexCoordPtr[i]->flags & flags)) {
300 ASSERT(VB->TexCoordPtr[i] == &inputs->TexCoord[i]);
301 _tnl_import_texcoord( ctx, i, writeable, stride );
302 }
303
304 if ((required & VERT_EDGE) && (VB->EdgeFlagPtr->flags & flags)) {
305 ASSERT(VB->EdgeFlagPtr == &inputs->EdgeFlag);
306 _tnl_import_edgeflag( ctx, writeable, stride );
307 }
308
309 VB->importable_data &= ~required;
310 }
311
312
313
314
315
316 void _tnl_vb_bind_arrays( GLcontext *ctx, GLint start, GLsizei count )
317 {
318 TNLcontext *tnl = TNL_CONTEXT(ctx);
319 struct vertex_buffer *VB = &tnl->vb;
320 GLuint inputs = tnl->pipeline.inputs;
321 GLuint imports;
322 struct vertex_arrays *tmp = &tnl->array_inputs;
323
324 if (ctx->Array.LockCount) {
325 ASSERT(start == ctx->Array.LockFirst);
326 ASSERT(count == 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 if (imports & VERT_EDGE) {
390 _tnl_import_edgeflag( ctx, 0, 0 );
391 tmp->EdgeFlag.count = VB->Count;
392 }
393 VB->EdgeFlagPtr = &tmp->EdgeFlag;
394 }
395
396 if (inputs & VERT_SPEC_RGB) {
397 if (imports & VERT_SPEC_RGB) {
398 _tnl_import_secondarycolor( ctx, 0, 0 );
399 tmp->SecondaryColor.count = VB->Count;
400 }
401 VB->SecondaryColorPtr[0] = &tmp->SecondaryColor;
402 VB->SecondaryColorPtr[1] = 0;
403 }
404
405 if (inputs & VERT_TEX_ANY) {
406 GLuint i;
407 for (i = 0; i < ctx->Const.MaxTextureUnits ; i++)
408 if (inputs & VERT_TEX(i)) {
409 if (imports & VERT_TEX(i)) {
410 _tnl_import_texcoord( ctx, i, 0, 0 );
411 tmp->TexCoord[i].count = VB->Count;
412 }
413 VB->TexCoordPtr[i] = &tmp->TexCoord[i];
414 }
415 }
416
417 VB->Primitive = tnl->tmp_primitive;
418 VB->PrimitiveLength = tnl->tmp_primitive_length;
419 VB->import_data = _tnl_upgrade_client_data;
420 VB->importable_data = imports & VERT_FIXUP;
421 /* _tnl_print_vert_flags("_tnl_vb_bind_arrays: importable", VB->importable_data); */
422
423 }
424
425
426
427
428 /* Function to fill an immediate struct with the effects of
429 * consecutive calls to ArrayElement with consecutive indices.
430 */
431 void _tnl_fill_immediate_drawarrays( GLcontext *ctx, struct immediate *IM,
432 GLuint start, GLuint count )
433 {
434 TNLcontext *tnl = TNL_CONTEXT(ctx);
435 GLuint required = ctx->Array._Enabled;
436 GLuint n = count - start;
437 GLuint i;
438
439 if (!ctx->CompileFlag)
440 required &= tnl->pipeline.inputs;
441
442 if (MESA_VERBOSE&VERBOSE_IMMEDIATE)
443 fprintf(stderr, "exec_full_array_elements %d .. %d\n", start, count);
444
445 _math_trans_4f( IM->Obj + IM->Start,
446 ctx->Array.Vertex.Ptr,
447 ctx->Array.Vertex.StrideB,
448 ctx->Array.Vertex.Type,
449 ctx->Array.Vertex.Size,
450 start, n );
451
452 if (ctx->Array.Vertex.Size == 4)
453 required |= VERT_OBJ_234;
454 else if (ctx->Array.Vertex.Size == 3)
455 required |= VERT_OBJ_23;
456
457
458 if (required & VERT_NORM) {
459 _math_trans_3f( IM->Normal + IM->Start,
460 ctx->Array.Normal.Ptr,
461 ctx->Array.Normal.StrideB,
462 ctx->Array.Normal.Type,
463 start, n );
464 }
465
466 if (required & VERT_EDGE) {
467 _math_trans_1ub( IM->EdgeFlag + IM->Start,
468 ctx->Array.EdgeFlag.Ptr,
469 ctx->Array.EdgeFlag.StrideB,
470 ctx->Array.EdgeFlag.Type,
471 start, n );
472 }
473
474 if (required & VERT_RGBA) {
475 _math_trans_4ub( IM->Color + IM->Start,
476 ctx->Array.Color.Ptr,
477 ctx->Array.Color.StrideB,
478 ctx->Array.Color.Type,
479 ctx->Array.Color.Size,
480 start, n );
481 }
482
483 if (required & VERT_SPEC_RGB) {
484 _math_trans_4ub( IM->SecondaryColor + IM->Start,
485 ctx->Array.SecondaryColor.Ptr,
486 ctx->Array.SecondaryColor.StrideB,
487 ctx->Array.SecondaryColor.Type,
488 ctx->Array.SecondaryColor.Size,
489 start, n );
490 }
491
492 if (required & VERT_FOG_COORD) {
493 _math_trans_1f( IM->FogCoord + IM->Start,
494 ctx->Array.FogCoord.Ptr,
495 ctx->Array.FogCoord.StrideB,
496 ctx->Array.FogCoord.Type,
497 start, n );
498 }
499
500 if (required & VERT_INDEX) {
501 _math_trans_1ui( IM->Index + IM->Start,
502 ctx->Array.Index.Ptr,
503 ctx->Array.Index.StrideB,
504 ctx->Array.Index.Type,
505 start, n );
506 }
507
508 if (required & VERT_TEX_ANY) {
509 for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++) {
510 if (required & VERT_TEX(i)) {
511 _math_trans_4f( IM->TexCoord[i] + IM->Start,
512 ctx->Array.TexCoord[i].Ptr,
513 ctx->Array.TexCoord[i].StrideB,
514 ctx->Array.TexCoord[i].Size,
515 ctx->Array.TexCoord[i].Type,
516 start, n );
517
518 if (ctx->Array.TexCoord[i].Size == 4)
519 IM->TexSize |= TEX_SIZE_4(i);
520 else if (ctx->Array.TexCoord[i].Size == 3)
521 IM->TexSize |= TEX_SIZE_3(i);
522 }
523 }
524 }
525
526 IM->Count = IM->Start + n;
527 IM->Flag[IM->Start] |= required;
528 for (i = IM->Start+1 ; i < IM->Count ; i++)
529 IM->Flag[i] = required;
530 }
531
532