Merge branch 'mesa_7_6_branch'
[mesa.git] / src / mesa / tnl / t_draw.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2007 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 * Authors:
25 * Keith Whitwell <keith@tungstengraphics.com>
26 */
27
28 #include "main/glheader.h"
29 #include "main/context.h"
30 #include "main/imports.h"
31 #include "main/state.h"
32 #include "main/mtypes.h"
33 #include "main/macros.h"
34 #include "main/enums.h"
35
36 #include "t_context.h"
37 #include "t_pipeline.h"
38 #include "t_vp_build.h"
39 #include "t_vertex.h"
40 #include "tnl.h"
41
42
43
44 static GLubyte *get_space(GLcontext *ctx, GLuint bytes)
45 {
46 TNLcontext *tnl = TNL_CONTEXT(ctx);
47 GLubyte *space = _mesa_malloc(bytes);
48
49 tnl->block[tnl->nr_blocks++] = space;
50 return space;
51 }
52
53
54 static void free_space(GLcontext *ctx)
55 {
56 TNLcontext *tnl = TNL_CONTEXT(ctx);
57 GLuint i;
58 for (i = 0; i < tnl->nr_blocks; i++)
59 _mesa_free(tnl->block[i]);
60 tnl->nr_blocks = 0;
61 }
62
63
64 /* Convert the incoming array to GLfloats. Understands the
65 * array->Normalized flag and selects the correct conversion method.
66 */
67 #define CONVERT( TYPE, MACRO ) do { \
68 GLuint i, j; \
69 if (input->Normalized) { \
70 for (i = 0; i < count; i++) { \
71 const TYPE *in = (TYPE *)ptr; \
72 for (j = 0; j < sz; j++) { \
73 *fptr++ = MACRO(*in); \
74 in++; \
75 } \
76 ptr += input->StrideB; \
77 } \
78 } else { \
79 for (i = 0; i < count; i++) { \
80 const TYPE *in = (TYPE *)ptr; \
81 for (j = 0; j < sz; j++) { \
82 *fptr++ = (GLfloat)(*in); \
83 in++; \
84 } \
85 ptr += input->StrideB; \
86 } \
87 } \
88 } while (0)
89
90
91 /**
92 * Convert array of BGRA/GLubyte[4] values to RGBA/float[4]
93 * \param ptr input/ubyte array
94 * \param fptr output/float array
95 */
96 static void
97 convert_bgra_to_float(const struct gl_client_array *input,
98 const GLubyte *ptr, GLfloat *fptr,
99 GLuint count )
100 {
101 GLuint i;
102 assert(input->Normalized);
103 assert(input->Size == 4);
104 for (i = 0; i < count; i++) {
105 const GLubyte *in = (GLubyte *) ptr; /* in is in BGRA order */
106 *fptr++ = UBYTE_TO_FLOAT(in[2]); /* red */
107 *fptr++ = UBYTE_TO_FLOAT(in[1]); /* green */
108 *fptr++ = UBYTE_TO_FLOAT(in[0]); /* blue */
109 *fptr++ = UBYTE_TO_FLOAT(in[3]); /* alpha */
110 ptr += input->StrideB;
111 }
112 }
113
114
115 /* Adjust pointer to point at first requested element, convert to
116 * floating point, populate VB->AttribPtr[].
117 */
118 static void _tnl_import_array( GLcontext *ctx,
119 GLuint attrib,
120 GLuint count,
121 const struct gl_client_array *input,
122 const GLubyte *ptr )
123 {
124 TNLcontext *tnl = TNL_CONTEXT(ctx);
125 struct vertex_buffer *VB = &tnl->vb;
126 GLuint stride = input->StrideB;
127
128 if (input->Type != GL_FLOAT) {
129 const GLuint sz = input->Size;
130 GLubyte *buf = get_space(ctx, count * sz * sizeof(GLfloat));
131 GLfloat *fptr = (GLfloat *)buf;
132
133 switch (input->Type) {
134 case GL_BYTE:
135 CONVERT(GLbyte, BYTE_TO_FLOAT);
136 break;
137 case GL_UNSIGNED_BYTE:
138 if (input->Format == GL_BGRA) {
139 /* See GL_EXT_vertex_array_bgra */
140 convert_bgra_to_float(input, ptr, fptr, count);
141 }
142 else {
143 CONVERT(GLubyte, UBYTE_TO_FLOAT);
144 }
145 break;
146 case GL_SHORT:
147 CONVERT(GLshort, SHORT_TO_FLOAT);
148 break;
149 case GL_UNSIGNED_SHORT:
150 CONVERT(GLushort, USHORT_TO_FLOAT);
151 break;
152 case GL_INT:
153 CONVERT(GLint, INT_TO_FLOAT);
154 break;
155 case GL_UNSIGNED_INT:
156 CONVERT(GLuint, UINT_TO_FLOAT);
157 break;
158 case GL_DOUBLE:
159 CONVERT(GLdouble, (GLfloat));
160 break;
161 default:
162 assert(0);
163 break;
164 }
165
166 ptr = buf;
167 stride = sz * sizeof(GLfloat);
168 }
169
170 VB->AttribPtr[attrib] = &tnl->tmp_inputs[attrib];
171 VB->AttribPtr[attrib]->data = (GLfloat (*)[4])ptr;
172 VB->AttribPtr[attrib]->start = (GLfloat *)ptr;
173 VB->AttribPtr[attrib]->count = count;
174 VB->AttribPtr[attrib]->stride = stride;
175 VB->AttribPtr[attrib]->size = input->Size;
176
177 /* This should die, but so should the whole GLvector4f concept:
178 */
179 VB->AttribPtr[attrib]->flags = (((1<<input->Size)-1) |
180 VEC_NOT_WRITEABLE |
181 (stride == 4*sizeof(GLfloat) ? 0 : VEC_BAD_STRIDE));
182
183 VB->AttribPtr[attrib]->storage = NULL;
184 }
185
186 #define CLIPVERTS ((6 + MAX_CLIP_PLANES) * 2)
187
188
189 static GLboolean *_tnl_import_edgeflag( GLcontext *ctx,
190 const GLvector4f *input,
191 GLuint count)
192 {
193 const GLubyte *ptr = (const GLubyte *)input->data;
194 const GLuint stride = input->stride;
195 GLboolean *space = (GLboolean *)get_space(ctx, count + CLIPVERTS);
196 GLboolean *bptr = space;
197 GLuint i;
198
199 for (i = 0; i < count; i++) {
200 *bptr++ = ((GLfloat *)ptr)[0] == 1.0;
201 ptr += stride;
202 }
203
204 return space;
205 }
206
207
208 static void bind_inputs( GLcontext *ctx,
209 const struct gl_client_array *inputs[],
210 GLint count,
211 struct gl_buffer_object **bo,
212 GLuint *nr_bo )
213 {
214 TNLcontext *tnl = TNL_CONTEXT(ctx);
215 struct vertex_buffer *VB = &tnl->vb;
216 GLuint i;
217
218 /* Map all the VBOs
219 */
220 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
221 const void *ptr;
222
223 if (inputs[i]->BufferObj->Name) {
224 if (!inputs[i]->BufferObj->Pointer) {
225 bo[*nr_bo] = inputs[i]->BufferObj;
226 (*nr_bo)++;
227 ctx->Driver.MapBuffer(ctx,
228 GL_ARRAY_BUFFER,
229 GL_READ_ONLY_ARB,
230 inputs[i]->BufferObj);
231
232 assert(inputs[i]->BufferObj->Pointer);
233 }
234
235 ptr = ADD_POINTERS(inputs[i]->BufferObj->Pointer,
236 inputs[i]->Ptr);
237 }
238 else
239 ptr = inputs[i]->Ptr;
240
241 /* Just make sure the array is floating point, otherwise convert to
242 * temporary storage.
243 *
244 * XXX: remove the GLvector4f type at some stage and just use
245 * client arrays.
246 */
247 _tnl_import_array(ctx, i, count, inputs[i], ptr);
248 }
249
250 /* We process only the vertices between min & max index:
251 */
252 VB->Count = count;
253
254
255 /* Legacy pointers -- remove one day.
256 */
257 VB->ObjPtr = VB->AttribPtr[_TNL_ATTRIB_POS];
258 VB->NormalPtr = VB->AttribPtr[_TNL_ATTRIB_NORMAL];
259 VB->ColorPtr[0] = VB->AttribPtr[_TNL_ATTRIB_COLOR0];
260 VB->ColorPtr[1] = NULL;
261 VB->IndexPtr[0] = VB->AttribPtr[_TNL_ATTRIB_COLOR_INDEX];
262 VB->IndexPtr[1] = NULL;
263 VB->SecondaryColorPtr[0] = VB->AttribPtr[_TNL_ATTRIB_COLOR1];
264 VB->SecondaryColorPtr[1] = NULL;
265 VB->FogCoordPtr = VB->AttribPtr[_TNL_ATTRIB_FOG];
266
267 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
268 VB->TexCoordPtr[i] = VB->AttribPtr[_TNL_ATTRIB_TEX0 + i];
269 }
270
271 /* Clipping and drawing code still requires this to be a packed
272 * array of ubytes which can be written into. TODO: Fix and
273 * remove.
274 */
275 if (ctx->Polygon.FrontMode != GL_FILL ||
276 ctx->Polygon.BackMode != GL_FILL)
277 {
278 VB->EdgeFlag = _tnl_import_edgeflag( ctx,
279 VB->AttribPtr[_TNL_ATTRIB_EDGEFLAG],
280 VB->Count );
281 }
282 else {
283 /* the data previously pointed to by EdgeFlag may have been freed */
284 VB->EdgeFlag = NULL;
285 }
286 }
287
288
289 /* Translate indices to GLuints and store in VB->Elts.
290 */
291 static void bind_indices( GLcontext *ctx,
292 const struct _mesa_index_buffer *ib,
293 struct gl_buffer_object **bo,
294 GLuint *nr_bo)
295 {
296 TNLcontext *tnl = TNL_CONTEXT(ctx);
297 struct vertex_buffer *VB = &tnl->vb;
298 GLuint i;
299 void *ptr;
300
301 if (!ib) {
302 VB->Elts = NULL;
303 return;
304 }
305
306 if (ib->obj->Name && !ib->obj->Pointer) {
307 bo[*nr_bo] = ib->obj;
308 (*nr_bo)++;
309 ctx->Driver.MapBuffer(ctx,
310 GL_ELEMENT_ARRAY_BUFFER,
311 GL_READ_ONLY_ARB,
312 ib->obj);
313
314 assert(ib->obj->Pointer);
315 }
316
317 ptr = ADD_POINTERS(ib->obj->Pointer, ib->ptr);
318
319 if (ib->type == GL_UNSIGNED_INT && VB->Primitive[0].basevertex == 0) {
320 VB->Elts = (GLuint *) ptr;
321 }
322 else {
323 GLuint *elts = (GLuint *)get_space(ctx, ib->count * sizeof(GLuint));
324 VB->Elts = elts;
325
326 if (ib->type == GL_UNSIGNED_INT) {
327 const GLuint *in = (GLuint *)ptr;
328 for (i = 0; i < ib->count; i++)
329 *elts++ = (GLuint)(*in++) + VB->Primitive[0].basevertex;
330 }
331 else if (ib->type == GL_UNSIGNED_SHORT) {
332 const GLushort *in = (GLushort *)ptr;
333 for (i = 0; i < ib->count; i++)
334 *elts++ = (GLuint)(*in++) + VB->Primitive[0].basevertex;
335 }
336 else {
337 const GLubyte *in = (GLubyte *)ptr;
338 for (i = 0; i < ib->count; i++)
339 *elts++ = (GLuint)(*in++) + VB->Primitive[0].basevertex;
340 }
341 }
342 }
343
344 static void bind_prims( GLcontext *ctx,
345 const struct _mesa_prim *prim,
346 GLuint nr_prims )
347 {
348 TNLcontext *tnl = TNL_CONTEXT(ctx);
349 struct vertex_buffer *VB = &tnl->vb;
350
351 VB->Primitive = prim;
352 VB->PrimitiveCount = nr_prims;
353 }
354
355 static void unmap_vbos( GLcontext *ctx,
356 struct gl_buffer_object **bo,
357 GLuint nr_bo )
358 {
359 GLuint i;
360 for (i = 0; i < nr_bo; i++) {
361 ctx->Driver.UnmapBuffer(ctx,
362 0, /* target -- I don't see why this would be needed */
363 bo[i]);
364 }
365 }
366
367
368 void _tnl_vbo_draw_prims(GLcontext *ctx,
369 const struct gl_client_array *arrays[],
370 const struct _mesa_prim *prim,
371 GLuint nr_prims,
372 const struct _mesa_index_buffer *ib,
373 GLboolean index_bounds_valid,
374 GLuint min_index,
375 GLuint max_index)
376 {
377 if (!index_bounds_valid)
378 vbo_get_minmax_index(ctx, prim, ib, &min_index, &max_index);
379
380 _tnl_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
381 }
382
383 /* This is the main entrypoint into the slimmed-down software tnl
384 * module. In a regular swtnl driver, this can be plugged straight
385 * into the vbo->Driver.DrawPrims() callback.
386 */
387 void _tnl_draw_prims( GLcontext *ctx,
388 const struct gl_client_array *arrays[],
389 const struct _mesa_prim *prim,
390 GLuint nr_prims,
391 const struct _mesa_index_buffer *ib,
392 GLuint min_index,
393 GLuint max_index)
394 {
395 TNLcontext *tnl = TNL_CONTEXT(ctx);
396 const GLuint TEST_SPLIT = 0;
397 const GLint max = TEST_SPLIT ? 8 : tnl->vb.Size - MAX_CLIPPED_VERTICES;
398 GLuint max_basevertex = prim->basevertex;
399 GLuint i;
400
401 for (i = 1; i < nr_prims; i++)
402 max_basevertex = MAX2(max_basevertex, prim[i].basevertex);
403
404 if (0)
405 {
406 _mesa_printf("%s %d..%d\n", __FUNCTION__, min_index, max_index);
407 for (i = 0; i < nr_prims; i++)
408 _mesa_printf("prim %d: %s start %d count %d\n", i,
409 _mesa_lookup_enum_by_nr(prim[i].mode),
410 prim[i].start,
411 prim[i].count);
412 }
413
414 if (min_index) {
415 /* We always translate away calls with min_index != 0.
416 */
417 vbo_rebase_prims( ctx, arrays, prim, nr_prims, ib,
418 min_index, max_index,
419 _tnl_vbo_draw_prims );
420 return;
421 }
422 else if (max_index + max_basevertex > max) {
423 /* The software TNL pipeline has a fixed amount of storage for
424 * vertices and it is necessary to split incoming drawing commands
425 * if they exceed that limit.
426 */
427 struct split_limits limits;
428 limits.max_verts = max;
429 limits.max_vb_size = ~0;
430 limits.max_indices = ~0;
431
432 /* This will split the buffers one way or another and
433 * recursively call back into this function.
434 */
435 vbo_split_prims( ctx, arrays, prim, nr_prims, ib,
436 0, max_index + prim->basevertex,
437 _tnl_vbo_draw_prims,
438 &limits );
439 }
440 else {
441 /* May need to map a vertex buffer object for every attribute plus
442 * one for the index buffer.
443 */
444 struct gl_buffer_object *bo[VERT_ATTRIB_MAX + 1];
445 GLuint nr_bo = 0;
446
447 for (i = 0; i < nr_prims;) {
448 GLuint this_nr_prims;
449
450 /* Our SW TNL pipeline doesn't handle basevertex yet, so bind_indices
451 * will rebase the elements to the basevertex, and we'll only
452 * emit strings of prims with the same basevertex in one draw call.
453 */
454 for (this_nr_prims = 1; i + this_nr_prims < nr_prims;
455 this_nr_prims++) {
456 if (prim[i].basevertex != prim[i + this_nr_prims].basevertex)
457 break;
458 }
459
460 /* Binding inputs may imply mapping some vertex buffer objects.
461 * They will need to be unmapped below.
462 */
463 bind_prims(ctx, &prim[i], this_nr_prims);
464 bind_inputs(ctx, arrays, max_index + prim[i].basevertex + 1,
465 bo, &nr_bo);
466 bind_indices(ctx, ib, bo, &nr_bo);
467
468 TNL_CONTEXT(ctx)->Driver.RunPipeline(ctx);
469
470 unmap_vbos(ctx, bo, nr_bo);
471 free_space(ctx);
472
473 i += this_nr_prims;
474 }
475 }
476 }
477