tnl: Push down the gl_vertex_array inputs into tnl drivers.
[mesa.git] / src / mesa / tnl / t_draw.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Keith Whitwell <keithw@vmware.com>
26 */
27
28 #include <stdio.h>
29
30 #include "main/glheader.h"
31 #include "main/bufferobj.h"
32 #include "main/condrender.h"
33 #include "main/context.h"
34 #include "main/imports.h"
35 #include "main/mtypes.h"
36 #include "main/macros.h"
37 #include "main/enums.h"
38 #include "main/varray.h"
39 #include "util/half_float.h"
40
41 #include "t_context.h"
42 #include "t_rebase.h"
43 #include "tnl.h"
44
45
46
47 static GLubyte *get_space(struct gl_context *ctx, GLuint bytes)
48 {
49 TNLcontext *tnl = TNL_CONTEXT(ctx);
50 GLubyte *space = malloc(bytes);
51
52 tnl->block[tnl->nr_blocks++] = space;
53 return space;
54 }
55
56
57 static void free_space(struct gl_context *ctx)
58 {
59 TNLcontext *tnl = TNL_CONTEXT(ctx);
60 GLuint i;
61 for (i = 0; i < tnl->nr_blocks; i++)
62 free(tnl->block[i]);
63 tnl->nr_blocks = 0;
64 }
65
66
67 /* Convert the incoming array to GLfloats. Understands the
68 * array->Normalized flag and selects the correct conversion method.
69 */
70 #define CONVERT( TYPE, MACRO ) do { \
71 GLuint i, j; \
72 if (attrib->Normalized) { \
73 for (i = 0; i < count; i++) { \
74 const TYPE *in = (TYPE *)ptr; \
75 for (j = 0; j < sz; j++) { \
76 *fptr++ = MACRO(*in); \
77 in++; \
78 } \
79 ptr += binding->Stride; \
80 } \
81 } else { \
82 for (i = 0; i < count; i++) { \
83 const TYPE *in = (TYPE *)ptr; \
84 for (j = 0; j < sz; j++) { \
85 *fptr++ = (GLfloat)(*in); \
86 in++; \
87 } \
88 ptr += binding->Stride; \
89 } \
90 } \
91 } while (0)
92
93
94 /**
95 * Convert array of BGRA/GLubyte[4] values to RGBA/float[4]
96 * \param ptr input/ubyte array
97 * \param fptr output/float array
98 */
99 static void
100 convert_bgra_to_float(const struct gl_vertex_buffer_binding *binding,
101 const struct gl_array_attributes *attrib,
102 const GLubyte *ptr, GLfloat *fptr,
103 GLuint count )
104 {
105 GLuint i;
106 assert(attrib->Normalized);
107 assert(attrib->Size == 4);
108 for (i = 0; i < count; i++) {
109 const GLubyte *in = (GLubyte *) ptr; /* in is in BGRA order */
110 *fptr++ = UBYTE_TO_FLOAT(in[2]); /* red */
111 *fptr++ = UBYTE_TO_FLOAT(in[1]); /* green */
112 *fptr++ = UBYTE_TO_FLOAT(in[0]); /* blue */
113 *fptr++ = UBYTE_TO_FLOAT(in[3]); /* alpha */
114 ptr += binding->Stride;
115 }
116 }
117
118 static void
119 convert_half_to_float(const struct gl_vertex_buffer_binding *binding,
120 const struct gl_array_attributes *attrib,
121 const GLubyte *ptr, GLfloat *fptr,
122 GLuint count, GLuint sz)
123 {
124 GLuint i, j;
125
126 for (i = 0; i < count; i++) {
127 GLhalfARB *in = (GLhalfARB *)ptr;
128
129 for (j = 0; j < sz; j++) {
130 *fptr++ = _mesa_half_to_float(in[j]);
131 }
132 ptr += binding->Stride;
133 }
134 }
135
136 /**
137 * \brief Convert fixed-point to floating-point.
138 *
139 * In OpenGL, a fixed-point number is a "signed 2's complement 16.16 scaled
140 * integer" (Table 2.2 of the OpenGL ES 2.0 spec).
141 *
142 * If the buffer has the \c normalized flag set, the formula
143 * \code normalize(x) := (2*x + 1) / (2^16 - 1) \endcode
144 * is used to map the fixed-point numbers into the range [-1, 1].
145 */
146 static void
147 convert_fixed_to_float(const struct gl_vertex_buffer_binding *binding,
148 const struct gl_array_attributes *attrib,
149 const GLubyte *ptr, GLfloat *fptr,
150 GLuint count)
151 {
152 GLuint i;
153 GLint j;
154 const GLint size = attrib->Size;
155
156 if (attrib->Normalized) {
157 for (i = 0; i < count; ++i) {
158 const GLfixed *in = (GLfixed *) ptr;
159 for (j = 0; j < size; ++j) {
160 *fptr++ = (GLfloat) (2 * in[j] + 1) / (GLfloat) ((1 << 16) - 1);
161 }
162 ptr += binding->Stride;
163 }
164 } else {
165 for (i = 0; i < count; ++i) {
166 const GLfixed *in = (GLfixed *) ptr;
167 for (j = 0; j < size; ++j) {
168 *fptr++ = in[j] / (GLfloat) (1 << 16);
169 }
170 ptr += binding->Stride;
171 }
172 }
173 }
174
175 /* Adjust pointer to point at first requested element, convert to
176 * floating point, populate VB->AttribPtr[].
177 */
178 static void _tnl_import_array( struct gl_context *ctx,
179 GLuint attr,
180 GLuint count,
181 const struct gl_vertex_buffer_binding *binding,
182 const struct gl_array_attributes *attrib,
183 const GLubyte *ptr )
184 {
185 TNLcontext *tnl = TNL_CONTEXT(ctx);
186 struct vertex_buffer *VB = &tnl->vb;
187 GLuint stride = binding->Stride;
188
189 if (attrib->Type != GL_FLOAT) {
190 const GLuint sz = attrib->Size;
191 GLubyte *buf = get_space(ctx, count * sz * sizeof(GLfloat));
192 GLfloat *fptr = (GLfloat *)buf;
193
194 switch (attrib->Type) {
195 case GL_BYTE:
196 CONVERT(GLbyte, BYTE_TO_FLOAT);
197 break;
198 case GL_UNSIGNED_BYTE:
199 if (attrib->Format == GL_BGRA) {
200 /* See GL_EXT_vertex_array_bgra */
201 convert_bgra_to_float(binding, attrib, ptr, fptr, count);
202 }
203 else {
204 CONVERT(GLubyte, UBYTE_TO_FLOAT);
205 }
206 break;
207 case GL_SHORT:
208 CONVERT(GLshort, SHORT_TO_FLOAT);
209 break;
210 case GL_UNSIGNED_SHORT:
211 CONVERT(GLushort, USHORT_TO_FLOAT);
212 break;
213 case GL_INT:
214 CONVERT(GLint, INT_TO_FLOAT);
215 break;
216 case GL_UNSIGNED_INT:
217 CONVERT(GLuint, UINT_TO_FLOAT);
218 break;
219 case GL_DOUBLE:
220 CONVERT(GLdouble, (GLfloat));
221 break;
222 case GL_HALF_FLOAT:
223 convert_half_to_float(binding, attrib, ptr, fptr, count, sz);
224 break;
225 case GL_FIXED:
226 convert_fixed_to_float(binding, attrib, ptr, fptr, count);
227 break;
228 default:
229 assert(0);
230 break;
231 }
232
233 ptr = buf;
234 stride = sz * sizeof(GLfloat);
235 }
236
237 VB->AttribPtr[attr] = &tnl->tmp_inputs[attr];
238 VB->AttribPtr[attr]->data = (GLfloat (*)[4])ptr;
239 VB->AttribPtr[attr]->start = (GLfloat *)ptr;
240 VB->AttribPtr[attr]->count = count;
241 VB->AttribPtr[attr]->stride = stride;
242 VB->AttribPtr[attr]->size = attrib->Size;
243
244 /* This should die, but so should the whole GLvector4f concept:
245 */
246 VB->AttribPtr[attr]->flags = (((1<<attrib->Size)-1) |
247 VEC_NOT_WRITEABLE |
248 (stride == 4*sizeof(GLfloat) ? 0 : VEC_BAD_STRIDE));
249
250 VB->AttribPtr[attr]->storage = NULL;
251 }
252
253 #define CLIPVERTS ((6 + MAX_CLIP_PLANES) * 2)
254
255
256 static GLboolean *_tnl_import_edgeflag( struct gl_context *ctx,
257 const GLvector4f *input,
258 GLuint count)
259 {
260 const GLubyte *ptr = (const GLubyte *)input->data;
261 const GLuint stride = input->stride;
262 GLboolean *space = (GLboolean *)get_space(ctx, count + CLIPVERTS);
263 GLboolean *bptr = space;
264 GLuint i;
265
266 for (i = 0; i < count; i++) {
267 *bptr++ = ((GLfloat *)ptr)[0] == 1.0F;
268 ptr += stride;
269 }
270
271 return space;
272 }
273
274
275 static void bind_inputs( struct gl_context *ctx,
276 const struct gl_vertex_array *inputs,
277 GLint count,
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
285 /* Map all the VBOs
286 */
287 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
288 const struct gl_vertex_array *array = &inputs[i];
289 const struct gl_vertex_buffer_binding *binding = array->BufferBinding;
290 const struct gl_array_attributes *attrib = array->VertexAttrib;
291 const void *ptr;
292
293 if (_mesa_is_bufferobj(binding->BufferObj)) {
294 if (!binding->BufferObj->Mappings[MAP_INTERNAL].Pointer) {
295 bo[*nr_bo] = binding->BufferObj;
296 (*nr_bo)++;
297 ctx->Driver.MapBufferRange(ctx, 0, binding->BufferObj->Size,
298 GL_MAP_READ_BIT,
299 binding->BufferObj,
300 MAP_INTERNAL);
301
302 assert(binding->BufferObj->Mappings[MAP_INTERNAL].Pointer);
303 }
304
305 ptr = ADD_POINTERS(binding->BufferObj->Mappings[MAP_INTERNAL].Pointer,
306 binding->Offset + attrib->RelativeOffset);
307 }
308 else
309 ptr = attrib->Ptr;
310
311 /* Just make sure the array is floating point, otherwise convert to
312 * temporary storage.
313 *
314 * XXX: remove the GLvector4f type at some stage and just use
315 * client arrays.
316 */
317 _tnl_import_array(ctx, i, count, binding, attrib, ptr);
318 }
319
320 /* We process only the vertices between min & max index:
321 */
322 VB->Count = count;
323
324 /* These should perhaps be part of _TNL_ATTRIB_* */
325 VB->BackfaceColorPtr = NULL;
326 VB->BackfaceIndexPtr = NULL;
327 VB->BackfaceSecondaryColorPtr = NULL;
328
329 /* Clipping and drawing code still requires this to be a packed
330 * array of ubytes which can be written into. TODO: Fix and
331 * remove.
332 */
333 if (ctx->Polygon.FrontMode != GL_FILL ||
334 ctx->Polygon.BackMode != GL_FILL)
335 {
336 VB->EdgeFlag = _tnl_import_edgeflag( ctx,
337 VB->AttribPtr[_TNL_ATTRIB_EDGEFLAG],
338 VB->Count );
339 }
340 else {
341 /* the data previously pointed to by EdgeFlag may have been freed */
342 VB->EdgeFlag = NULL;
343 }
344 }
345
346
347 /* Translate indices to GLuints and store in VB->Elts.
348 */
349 static void bind_indices( struct gl_context *ctx,
350 const struct _mesa_index_buffer *ib,
351 struct gl_buffer_object **bo,
352 GLuint *nr_bo)
353 {
354 TNLcontext *tnl = TNL_CONTEXT(ctx);
355 struct vertex_buffer *VB = &tnl->vb;
356 GLuint i;
357 const void *ptr;
358
359 if (!ib) {
360 VB->Elts = NULL;
361 return;
362 }
363
364 if (_mesa_is_bufferobj(ib->obj) &&
365 !_mesa_bufferobj_mapped(ib->obj, MAP_INTERNAL)) {
366 /* if the buffer object isn't mapped yet, map it now */
367 bo[*nr_bo] = ib->obj;
368 (*nr_bo)++;
369 ptr = ctx->Driver.MapBufferRange(ctx, (GLsizeiptr) ib->ptr,
370 ib->count * ib->index_size,
371 GL_MAP_READ_BIT, ib->obj,
372 MAP_INTERNAL);
373 assert(ib->obj->Mappings[MAP_INTERNAL].Pointer);
374 } else {
375 /* user-space elements, or buffer already mapped */
376 ptr = ADD_POINTERS(ib->obj->Mappings[MAP_INTERNAL].Pointer, ib->ptr);
377 }
378
379 if (ib->index_size == 4 && VB->Primitive[0].basevertex == 0) {
380 VB->Elts = (GLuint *) ptr;
381 }
382 else {
383 GLuint *elts = (GLuint *)get_space(ctx, ib->count * sizeof(GLuint));
384 VB->Elts = elts;
385
386 if (ib->index_size == 4) {
387 const GLuint *in = (GLuint *)ptr;
388 for (i = 0; i < ib->count; i++)
389 *elts++ = (GLuint)(*in++) + VB->Primitive[0].basevertex;
390 }
391 else if (ib->index_size == 2) {
392 const GLushort *in = (GLushort *)ptr;
393 for (i = 0; i < ib->count; i++)
394 *elts++ = (GLuint)(*in++) + VB->Primitive[0].basevertex;
395 }
396 else {
397 const GLubyte *in = (GLubyte *)ptr;
398 for (i = 0; i < ib->count; i++)
399 *elts++ = (GLuint)(*in++) + VB->Primitive[0].basevertex;
400 }
401 }
402 }
403
404 static void bind_prims( struct gl_context *ctx,
405 const struct _mesa_prim *prim,
406 GLuint nr_prims )
407 {
408 TNLcontext *tnl = TNL_CONTEXT(ctx);
409 struct vertex_buffer *VB = &tnl->vb;
410
411 VB->Primitive = prim;
412 VB->PrimitiveCount = nr_prims;
413 }
414
415 static void unmap_vbos( struct gl_context *ctx,
416 struct gl_buffer_object **bo,
417 GLuint nr_bo )
418 {
419 GLuint i;
420 for (i = 0; i < nr_bo; i++) {
421 ctx->Driver.UnmapBuffer(ctx, bo[i], MAP_INTERNAL);
422 }
423 }
424
425
426 /* This is the main workhorse doing all the rendering work.
427 */
428 void _tnl_draw_prims(struct gl_context *ctx,
429 const struct _mesa_prim *prim,
430 GLuint nr_prims,
431 const struct _mesa_index_buffer *ib,
432 GLboolean index_bounds_valid,
433 GLuint min_index,
434 GLuint max_index,
435 struct gl_transform_feedback_object *tfb_vertcount,
436 unsigned stream,
437 struct gl_buffer_object *indirect)
438 {
439 TNLcontext *tnl = TNL_CONTEXT(ctx);
440 const struct gl_vertex_array *arrays = ctx->Array._DrawArrays;
441 const GLuint TEST_SPLIT = 0;
442 const GLint max = TEST_SPLIT ? 8 : tnl->vb.Size - MAX_CLIPPED_VERTICES;
443 GLint max_basevertex = prim->basevertex;
444 GLuint i;
445
446 if (!index_bounds_valid)
447 vbo_get_minmax_indices(ctx, prim, ib, &min_index, &max_index, nr_prims);
448
449 /* Mesa core state should have been validated already */
450 assert(ctx->NewState == 0x0);
451
452 if (!_mesa_check_conditional_render(ctx))
453 return; /* don't draw */
454
455 for (i = 1; i < nr_prims; i++)
456 max_basevertex = MAX2(max_basevertex, prim[i].basevertex);
457
458 if (0)
459 {
460 printf("%s %d..%d\n", __func__, min_index, max_index);
461 for (i = 0; i < nr_prims; i++)
462 printf("prim %d: %s start %d count %d\n", i,
463 _mesa_enum_to_string(prim[i].mode),
464 prim[i].start,
465 prim[i].count);
466 }
467
468 if (min_index) {
469 /* We always translate away calls with min_index != 0.
470 */
471 t_rebase_prims( ctx, arrays, prim, nr_prims, ib,
472 min_index, max_index,
473 _tnl_draw_prims );
474 return;
475 }
476 else if ((GLint)max_index + max_basevertex > max) {
477 /* The software TNL pipeline has a fixed amount of storage for
478 * vertices and it is necessary to split incoming drawing commands
479 * if they exceed that limit.
480 */
481 struct split_limits limits;
482 limits.max_verts = max;
483 limits.max_vb_size = ~0;
484 limits.max_indices = ~0;
485
486 /* This will split the buffers one way or another and
487 * recursively call back into this function.
488 */
489 vbo_split_prims( ctx, arrays, prim, nr_prims, ib,
490 0, max_index + prim->basevertex,
491 _tnl_draw_prims,
492 &limits );
493 }
494 else {
495 /* May need to map a vertex buffer object for every attribute plus
496 * one for the index buffer.
497 */
498 struct gl_buffer_object *bo[VERT_ATTRIB_MAX + 1];
499 GLuint nr_bo = 0;
500 GLuint inst;
501
502 for (i = 0; i < nr_prims;) {
503 GLuint this_nr_prims;
504
505 /* Our SW TNL pipeline doesn't handle basevertex yet, so bind_indices
506 * will rebase the elements to the basevertex, and we'll only
507 * emit strings of prims with the same basevertex in one draw call.
508 */
509 for (this_nr_prims = 1; i + this_nr_prims < nr_prims;
510 this_nr_prims++) {
511 if (prim[i].basevertex != prim[i + this_nr_prims].basevertex)
512 break;
513 }
514
515 assert(prim[i].num_instances > 0);
516
517 /* Binding inputs may imply mapping some vertex buffer objects.
518 * They will need to be unmapped below.
519 */
520 for (inst = 0; inst < prim[i].num_instances; inst++) {
521
522 bind_prims(ctx, &prim[i], this_nr_prims);
523 bind_inputs(ctx, arrays, max_index + prim[i].basevertex + 1,
524 bo, &nr_bo);
525 bind_indices(ctx, ib, bo, &nr_bo);
526
527 tnl->CurInstance = inst;
528 TNL_CONTEXT(ctx)->Driver.RunPipeline(ctx);
529
530 unmap_vbos(ctx, bo, nr_bo);
531 free_space(ctx);
532 }
533
534 i += this_nr_prims;
535 }
536 }
537 }
538
539
540 void
541 _tnl_bind_inputs( struct gl_context *ctx )
542 {
543 TNLcontext *tnl = TNL_CONTEXT(ctx);
544 _mesa_set_drawing_arrays(ctx, tnl->draw_arrays.inputs);
545 _vbo_update_inputs(ctx, &tnl->draw_arrays);
546 }
547
548
549 /* This is the main entrypoint into the slimmed-down software tnl
550 * module. In a regular swtnl driver, this can be plugged straight
551 * into the ctx->Driver.Draw() callback.
552 */
553 void
554 _tnl_draw(struct gl_context *ctx,
555 const struct _mesa_prim *prim, GLuint nr_prims,
556 const struct _mesa_index_buffer *ib,
557 GLboolean index_bounds_valid, GLuint min_index, GLuint max_index,
558 struct gl_transform_feedback_object *tfb_vertcount,
559 unsigned stream, struct gl_buffer_object *indirect)
560 {
561 /* Update TNLcontext::draw_arrays and set that pointer
562 * into Array._DrawArrays.
563 */
564 _tnl_bind_inputs(ctx);
565
566 _tnl_draw_prims(ctx, prim, nr_prims, ib, index_bounds_valid,
567 min_index, max_index, tfb_vertcount, stream, indirect);
568 }
569
570
571 void
572 _tnl_init_driver_draw_function(struct dd_function_table *functions)
573 {
574 functions->Draw = _tnl_draw;
575 }