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