1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
/*
* Mesa 3-D graphics library
* Version: 6.5
*
* Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Authors:
* Keith Whitwell <keith@tungstengraphics.com>
*/
#include "glheader.h"
#include "context.h"
#include "imports.h"
#include "state.h"
#include "mtypes.h"
#include "macros.h"
#include "enums.h"
#include "t_context.h"
#include "t_pipeline.h"
#include "t_vp_build.h"
#include "t_vertex.h"
#include "tnl.h"
static GLubyte *get_space(GLcontext *ctx, GLuint bytes)
{
TNLcontext *tnl = TNL_CONTEXT(ctx);
GLubyte *space = _mesa_malloc(bytes);
tnl->block[tnl->nr_blocks++] = space;
return space;
}
static void free_space(GLcontext *ctx)
{
TNLcontext *tnl = TNL_CONTEXT(ctx);
GLuint i;
for (i = 0; i < tnl->nr_blocks; i++)
_mesa_free(tnl->block[i]);
tnl->nr_blocks = 0;
}
/* Convert the incoming array to GLfloats. Understands the
* array->Normalized flag and selects the correct conversion method.
*/
#define CONVERT( TYPE, MACRO ) do { \
GLuint i, j; \
if (input->Normalized) { \
for (i = 0; i < count; i++) { \
const TYPE *in = (TYPE *)ptr; \
for (j = 0; j < sz; j++) { \
*fptr++ = MACRO(*in); \
in++; \
} \
ptr += input->StrideB; \
} \
} else { \
for (i = 0; i < count; i++) { \
const TYPE *in = (TYPE *)ptr; \
for (j = 0; j < sz; j++) { \
*fptr++ = (GLfloat)(*in); \
in++; \
} \
ptr += input->StrideB; \
} \
} \
} while (0)
/* Adjust pointer to point at first requested element, convert to
* floating point, populate VB->AttribPtr[].
*/
static void _tnl_import_array( GLcontext *ctx,
GLuint attrib,
GLuint start,
GLuint end,
const struct gl_client_array *input,
const GLubyte *ptr )
{
TNLcontext *tnl = TNL_CONTEXT(ctx);
struct vertex_buffer *VB = &tnl->vb;
const GLuint count = end - start;
GLuint stride = input->StrideB;
ptr += start * stride;
if (input->Type != GL_FLOAT) {
const GLuint sz = input->Size;
GLubyte *buf = get_space(ctx, count * sz * sizeof(GLfloat));
GLfloat *fptr = (GLfloat *)buf;
switch (input->Type) {
case GL_BYTE:
CONVERT(GLbyte, BYTE_TO_FLOAT);
break;
case GL_UNSIGNED_BYTE:
CONVERT(GLubyte, UBYTE_TO_FLOAT);
break;
case GL_SHORT:
CONVERT(GLshort, SHORT_TO_FLOAT);
break;
case GL_UNSIGNED_SHORT:
CONVERT(GLushort, USHORT_TO_FLOAT);
break;
case GL_INT:
CONVERT(GLint, INT_TO_FLOAT);
break;
case GL_UNSIGNED_INT:
CONVERT(GLuint, UINT_TO_FLOAT);
break;
case GL_DOUBLE:
CONVERT(GLdouble, (GLfloat));
break;
default:
assert(0);
break;
}
ptr = buf;
stride = sz * sizeof(GLfloat);
}
VB->AttribPtr[attrib] = &tnl->tmp_inputs[attrib];
VB->AttribPtr[attrib]->data = (GLfloat (*)[4])ptr;
VB->AttribPtr[attrib]->start = (GLfloat *)ptr;
VB->AttribPtr[attrib]->count = count;
VB->AttribPtr[attrib]->stride = stride;
VB->AttribPtr[attrib]->size = input->Size;
/* This should die, but so should the whole GLvector4f concept:
*/
VB->AttribPtr[attrib]->flags = (((1<<input->Size)-1) |
VEC_NOT_WRITEABLE |
(stride == 4*sizeof(GLfloat) ? 0 : VEC_BAD_STRIDE));
VB->AttribPtr[attrib]->storage = NULL;
}
#define CLIPVERTS ((6 + MAX_CLIP_PLANES) * 2)
static GLboolean *_tnl_import_edgeflag( GLcontext *ctx,
const GLvector4f *input,
GLuint count)
{
const GLubyte *ptr = (const GLubyte *)input->data;
const GLuint stride = input->stride;
GLboolean *space = (GLboolean *)get_space(ctx, count + CLIPVERTS);
GLboolean *bptr = space;
GLuint i;
for (i = 0; i < count; i++) {
*bptr++ = ((GLfloat *)ptr)[0] == 1.0;
ptr += stride;
}
return space;
}
static void bind_inputs( GLcontext *ctx,
const struct gl_client_array *inputs[],
GLint min_index, GLint max_index,
struct gl_buffer_object **bo,
GLuint *nr_bo )
{
TNLcontext *tnl = TNL_CONTEXT(ctx);
struct vertex_buffer *VB = &tnl->vb;
GLuint i;
/* Map all the VBOs
*/
for (i = 0; i < VERT_ATTRIB_MAX; i++) {
const void *ptr;
if (inputs[i]->BufferObj->Name) {
if (!inputs[i]->BufferObj->Pointer) {
bo[*nr_bo] = inputs[i]->BufferObj;
(*nr_bo)++;
ctx->Driver.MapBuffer(ctx,
GL_ARRAY_BUFFER,
GL_READ_ONLY_ARB,
inputs[i]->BufferObj);
assert(inputs[i]->BufferObj->Pointer);
}
ptr = ADD_POINTERS(inputs[i]->BufferObj->Pointer,
inputs[i]->Ptr);
}
else
ptr = inputs[i]->Ptr;
/* Just make sure the array is floating point, otherwise convert to
* temporary storage. Rebase arrays so that 'min_index' becomes
* element zero.
*
* XXX: remove the GLvector4f type at some stage and just use
* client arrays.
*/
_tnl_import_array(ctx, i, min_index, max_index, inputs[i], ptr);
}
/* We process only the vertices between min & max index:
*/
VB->Count = max_index - min_index;
/* Legacy pointers -- remove one day.
*/
VB->ObjPtr = VB->AttribPtr[_TNL_ATTRIB_POS];
VB->NormalPtr = VB->AttribPtr[_TNL_ATTRIB_NORMAL];
VB->ColorPtr[0] = VB->AttribPtr[_TNL_ATTRIB_COLOR0];
VB->ColorPtr[1] = NULL;
VB->IndexPtr[0] = VB->AttribPtr[_TNL_ATTRIB_COLOR_INDEX];
VB->IndexPtr[1] = NULL;
VB->SecondaryColorPtr[0] = VB->AttribPtr[_TNL_ATTRIB_COLOR1];
VB->SecondaryColorPtr[1] = NULL;
VB->FogCoordPtr = VB->AttribPtr[_TNL_ATTRIB_FOG];
for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
VB->TexCoordPtr[i] = VB->AttribPtr[_TNL_ATTRIB_TEX0 + i];
}
/* Clipping and drawing code still requires this to be a packed
* array of ubytes which can be written into. TODO: Fix and
* remove.
*/
if (ctx->Polygon.FrontMode != GL_FILL ||
ctx->Polygon.BackMode != GL_FILL)
{
VB->EdgeFlag = _tnl_import_edgeflag( ctx,
VB->AttribPtr[_TNL_ATTRIB_EDGEFLAG],
VB->Count );
}
}
/* Translate indices to GLuints and store in VB->Elts.
*/
static void bind_indices( GLcontext *ctx,
const struct _mesa_index_buffer *ib,
GLuint min_index,
struct gl_buffer_object **bo,
GLuint *nr_bo)
{
TNLcontext *tnl = TNL_CONTEXT(ctx);
struct vertex_buffer *VB = &tnl->vb;
GLuint i;
void *ptr;
if (!ib)
return;
if (ib->obj->Name && !ib->obj->Pointer) {
bo[*nr_bo] = ib->obj;
(*nr_bo)++;
ctx->Driver.MapBuffer(ctx,
GL_ELEMENT_ARRAY_BUFFER,
GL_READ_ONLY_ARB,
ib->obj);
assert(ib->obj->Pointer);
}
ptr = ADD_POINTERS(ib->obj->Pointer, ib->ptr);
if (ib->type == GL_UNSIGNED_INT && min_index == 0) {
VB->Elts = (GLuint *) ptr;
VB->Elts += ib->rebase;
}
else {
GLuint *elts = (GLuint *)get_space(ctx, ib->count * sizeof(GLuint));
VB->Elts = elts;
switch (ib->type) {
case GL_UNSIGNED_INT: {
const GLuint *in = ((GLuint *)ptr) + ib->rebase;
for (i = 0; i < ib->count; i++)
*elts++ = *in++ - min_index;
break;
}
case GL_UNSIGNED_SHORT: {
const GLushort *in = ((GLushort *)ptr) + ib->rebase;
for (i = 0; i < ib->count; i++)
*elts++ = (GLuint)(*in++) - min_index;
break;
}
case GL_UNSIGNED_BYTE: {
const GLubyte *in = ((GLubyte *)ptr) + ib->rebase;
for (i = 0; i < ib->count; i++)
*elts++ = (GLuint)(*in++) - min_index;
break;
}
}
}
}
static void bind_prims( GLcontext *ctx,
const struct _mesa_prim *prim,
GLuint nr_prims,
GLuint min_index )
{
TNLcontext *tnl = TNL_CONTEXT(ctx);
struct vertex_buffer *VB = &tnl->vb;
GLuint i;
if (min_index != 0) {
struct _mesa_prim *tmp = (struct _mesa_prim *)get_space(ctx, nr_prims * sizeof(*prim));
for (i = 0; i < nr_prims; i++) {
tmp[i] = prim[i];
tmp[i].start -= min_index;
}
VB->Primitive = tmp;
}
else {
VB->Primitive = prim;
}
VB->PrimitiveCount = nr_prims;
}
static void unmap_vbos( GLcontext *ctx,
struct gl_buffer_object **bo,
GLuint nr_bo )
{
GLuint i;
for (i = 0; i < nr_bo; i++) {
ctx->Driver.UnmapBuffer(ctx,
0, /* target -- I don't see why this would be needed */
bo[i]);
}
}
/* This is the main entrypoint into the slimmed-down software tnl
* module. In a regular swtnl driver, this can be plugged straight
* into the vbo->Driver.DrawPrims() callback.
*/
void _tnl_draw_prims( GLcontext *ctx,
const struct gl_client_array *arrays[],
const struct _mesa_prim *prim,
GLuint nr_prims,
const struct _mesa_index_buffer *ib,
GLuint min_index,
GLuint max_index)
{
TNLcontext *tnl = TNL_CONTEXT(ctx);
struct vertex_buffer *VB = &tnl->vb;
GLint max = VB->Size;
GLuint i;
#ifdef TEST_SPLIT
max = 8 + MAX_CLIPPED_VERTICES;
#endif
assert(max_index > min_index);
assert(!(max_index & 0x80000000));
VB->Elts = NULL;
#if 0
_mesa_printf("%s %d..%d\n", __FUNCTION__, min_index, max_index);
for (i = 0; i < nr_prims; i++)
_mesa_printf("prim %d: %s start %d count %d\n", i,
_mesa_lookup_enum_by_nr(prim[i].mode),
prim[i].start,
prim[i].count);
#endif
/* The software TNL pipeline has a fixed amount of storage for
* vertices and it is necessary to split incoming drawing commands
* if they exceed that limit.
*/
if (max_index - min_index >= max - MAX_CLIPPED_VERTICES) {
struct split_limits limits;
limits.max_verts = max - MAX_CLIPPED_VERTICES;
limits.max_vb_size = ~0;
limits.max_indices = ~0;
/* This will split the buffers one way or another and
* recursively call back into this function.
*/
vbo_split_prims( ctx, arrays, prim, nr_prims, ib,
min_index, max_index,
_tnl_draw_prims,
&limits );
}
else {
/* May need to map a vertex buffer object for every attribute plus
* one for the index buffer.
*/
struct gl_buffer_object *bo[VERT_ATTRIB_MAX + 1];
GLuint nr_bo = 0;
/* Binding inputs may imply mapping some vertex buffer objects.
* They will need to be unmapped below.
*/
bind_inputs(ctx, arrays, min_index, max_index+1, bo, &nr_bo);
bind_indices(ctx, ib, min_index, bo, &nr_bo);
bind_prims(ctx, prim, nr_prims, VB->Elts ? 0 : min_index );
TNL_CONTEXT(ctx)->Driver.RunPipeline(ctx);
unmap_vbos(ctx, bo, nr_bo);
free_space(ctx);
}
}
|