mesa/vbo/tnl: Move gl_vertex_array related stuff to tnl.
[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/arrayobj.h"
32 #include "main/bufferobj.h"
33 #include "main/condrender.h"
34 #include "main/context.h"
35 #include "main/imports.h"
36 #include "main/mtypes.h"
37 #include "main/macros.h"
38 #include "main/enums.h"
39 #include "main/varray.h"
40 #include "util/half_float.h"
41
42 #include "t_context.h"
43 #include "t_rebase.h"
44 #include "tnl.h"
45
46
47
48 static GLubyte *get_space(struct gl_context *ctx, GLuint bytes)
49 {
50 TNLcontext *tnl = TNL_CONTEXT(ctx);
51 GLubyte *space = malloc(bytes);
52
53 tnl->block[tnl->nr_blocks++] = space;
54 return space;
55 }
56
57
58 static void free_space(struct gl_context *ctx)
59 {
60 TNLcontext *tnl = TNL_CONTEXT(ctx);
61 GLuint i;
62 for (i = 0; i < tnl->nr_blocks; i++)
63 free(tnl->block[i]);
64 tnl->nr_blocks = 0;
65 }
66
67
68 /* Convert the incoming array to GLfloats. Understands the
69 * array->Normalized flag and selects the correct conversion method.
70 */
71 #define CONVERT( TYPE, MACRO ) do { \
72 GLuint i, j; \
73 if (attrib->Normalized) { \
74 for (i = 0; i < count; i++) { \
75 const TYPE *in = (TYPE *)ptr; \
76 for (j = 0; j < sz; j++) { \
77 *fptr++ = MACRO(*in); \
78 in++; \
79 } \
80 ptr += binding->Stride; \
81 } \
82 } else { \
83 for (i = 0; i < count; i++) { \
84 const TYPE *in = (TYPE *)ptr; \
85 for (j = 0; j < sz; j++) { \
86 *fptr++ = (GLfloat)(*in); \
87 in++; \
88 } \
89 ptr += binding->Stride; \
90 } \
91 } \
92 } while (0)
93
94
95 /**
96 * Convert array of BGRA/GLubyte[4] values to RGBA/float[4]
97 * \param ptr input/ubyte array
98 * \param fptr output/float array
99 */
100 static void
101 convert_bgra_to_float(const struct gl_vertex_buffer_binding *binding,
102 const struct gl_array_attributes *attrib,
103 const GLubyte *ptr, GLfloat *fptr,
104 GLuint count )
105 {
106 GLuint i;
107 assert(attrib->Normalized);
108 assert(attrib->Size == 4);
109 for (i = 0; i < count; i++) {
110 const GLubyte *in = (GLubyte *) ptr; /* in is in BGRA order */
111 *fptr++ = UBYTE_TO_FLOAT(in[2]); /* red */
112 *fptr++ = UBYTE_TO_FLOAT(in[1]); /* green */
113 *fptr++ = UBYTE_TO_FLOAT(in[0]); /* blue */
114 *fptr++ = UBYTE_TO_FLOAT(in[3]); /* alpha */
115 ptr += binding->Stride;
116 }
117 }
118
119 static void
120 convert_half_to_float(const struct gl_vertex_buffer_binding *binding,
121 const struct gl_array_attributes *attrib,
122 const GLubyte *ptr, GLfloat *fptr,
123 GLuint count, GLuint sz)
124 {
125 GLuint i, j;
126
127 for (i = 0; i < count; i++) {
128 GLhalfARB *in = (GLhalfARB *)ptr;
129
130 for (j = 0; j < sz; j++) {
131 *fptr++ = _mesa_half_to_float(in[j]);
132 }
133 ptr += binding->Stride;
134 }
135 }
136
137 /**
138 * \brief Convert fixed-point to floating-point.
139 *
140 * In OpenGL, a fixed-point number is a "signed 2's complement 16.16 scaled
141 * integer" (Table 2.2 of the OpenGL ES 2.0 spec).
142 *
143 * If the buffer has the \c normalized flag set, the formula
144 * \code normalize(x) := (2*x + 1) / (2^16 - 1) \endcode
145 * is used to map the fixed-point numbers into the range [-1, 1].
146 */
147 static void
148 convert_fixed_to_float(const struct gl_vertex_buffer_binding *binding,
149 const struct gl_array_attributes *attrib,
150 const GLubyte *ptr, GLfloat *fptr,
151 GLuint count)
152 {
153 GLuint i;
154 GLint j;
155 const GLint size = attrib->Size;
156
157 if (attrib->Normalized) {
158 for (i = 0; i < count; ++i) {
159 const GLfixed *in = (GLfixed *) ptr;
160 for (j = 0; j < size; ++j) {
161 *fptr++ = (GLfloat) (2 * in[j] + 1) / (GLfloat) ((1 << 16) - 1);
162 }
163 ptr += binding->Stride;
164 }
165 } else {
166 for (i = 0; i < count; ++i) {
167 const GLfixed *in = (GLfixed *) ptr;
168 for (j = 0; j < size; ++j) {
169 *fptr++ = in[j] / (GLfloat) (1 << 16);
170 }
171 ptr += binding->Stride;
172 }
173 }
174 }
175
176 /* Adjust pointer to point at first requested element, convert to
177 * floating point, populate VB->AttribPtr[].
178 */
179 static void _tnl_import_array( struct gl_context *ctx,
180 GLuint attr,
181 GLuint count,
182 const struct gl_vertex_buffer_binding *binding,
183 const struct gl_array_attributes *attrib,
184 const GLubyte *ptr )
185 {
186 TNLcontext *tnl = TNL_CONTEXT(ctx);
187 struct vertex_buffer *VB = &tnl->vb;
188 GLuint stride = binding->Stride;
189
190 if (attrib->Type != GL_FLOAT) {
191 const GLuint sz = attrib->Size;
192 GLubyte *buf = get_space(ctx, count * sz * sizeof(GLfloat));
193 GLfloat *fptr = (GLfloat *)buf;
194
195 switch (attrib->Type) {
196 case GL_BYTE:
197 CONVERT(GLbyte, BYTE_TO_FLOAT);
198 break;
199 case GL_UNSIGNED_BYTE:
200 if (attrib->Format == GL_BGRA) {
201 /* See GL_EXT_vertex_array_bgra */
202 convert_bgra_to_float(binding, attrib, ptr, fptr, count);
203 }
204 else {
205 CONVERT(GLubyte, UBYTE_TO_FLOAT);
206 }
207 break;
208 case GL_SHORT:
209 CONVERT(GLshort, SHORT_TO_FLOAT);
210 break;
211 case GL_UNSIGNED_SHORT:
212 CONVERT(GLushort, USHORT_TO_FLOAT);
213 break;
214 case GL_INT:
215 CONVERT(GLint, INT_TO_FLOAT);
216 break;
217 case GL_UNSIGNED_INT:
218 CONVERT(GLuint, UINT_TO_FLOAT);
219 break;
220 case GL_DOUBLE:
221 CONVERT(GLdouble, (GLfloat));
222 break;
223 case GL_HALF_FLOAT:
224 convert_half_to_float(binding, attrib, ptr, fptr, count, sz);
225 break;
226 case GL_FIXED:
227 convert_fixed_to_float(binding, attrib, ptr, fptr, count);
228 break;
229 default:
230 assert(0);
231 break;
232 }
233
234 ptr = buf;
235 stride = sz * sizeof(GLfloat);
236 }
237
238 VB->AttribPtr[attr] = &tnl->tmp_inputs[attr];
239 VB->AttribPtr[attr]->data = (GLfloat (*)[4])ptr;
240 VB->AttribPtr[attr]->start = (GLfloat *)ptr;
241 VB->AttribPtr[attr]->count = count;
242 VB->AttribPtr[attr]->stride = stride;
243 VB->AttribPtr[attr]->size = attrib->Size;
244
245 /* This should die, but so should the whole GLvector4f concept:
246 */
247 VB->AttribPtr[attr]->flags = (((1<<attrib->Size)-1) |
248 VEC_NOT_WRITEABLE |
249 (stride == 4*sizeof(GLfloat) ? 0 : VEC_BAD_STRIDE));
250
251 VB->AttribPtr[attr]->storage = NULL;
252 }
253
254 #define CLIPVERTS ((6 + MAX_CLIP_PLANES) * 2)
255
256
257 static GLboolean *_tnl_import_edgeflag( struct gl_context *ctx,
258 const GLvector4f *input,
259 GLuint count)
260 {
261 const GLubyte *ptr = (const GLubyte *)input->data;
262 const GLuint stride = input->stride;
263 GLboolean *space = (GLboolean *)get_space(ctx, count + CLIPVERTS);
264 GLboolean *bptr = space;
265 GLuint i;
266
267 for (i = 0; i < count; i++) {
268 *bptr++ = ((GLfloat *)ptr)[0] == 1.0F;
269 ptr += stride;
270 }
271
272 return space;
273 }
274
275
276 static void bind_inputs( struct gl_context *ctx,
277 const struct tnl_vertex_array *inputs,
278 GLint count,
279 struct gl_buffer_object **bo,
280 GLuint *nr_bo )
281 {
282 TNLcontext *tnl = TNL_CONTEXT(ctx);
283 struct vertex_buffer *VB = &tnl->vb;
284 GLuint i;
285
286 /* Map all the VBOs
287 */
288 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
289 const struct tnl_vertex_array *array = &inputs[i];
290 const struct gl_vertex_buffer_binding *binding = array->BufferBinding;
291 const struct gl_array_attributes *attrib = array->VertexAttrib;
292 const void *ptr;
293
294 if (_mesa_is_bufferobj(binding->BufferObj)) {
295 if (!binding->BufferObj->Mappings[MAP_INTERNAL].Pointer) {
296 bo[*nr_bo] = binding->BufferObj;
297 (*nr_bo)++;
298 ctx->Driver.MapBufferRange(ctx, 0, binding->BufferObj->Size,
299 GL_MAP_READ_BIT,
300 binding->BufferObj,
301 MAP_INTERNAL);
302
303 assert(binding->BufferObj->Mappings[MAP_INTERNAL].Pointer);
304 }
305
306 ptr = ADD_POINTERS(binding->BufferObj->Mappings[MAP_INTERNAL].Pointer,
307 binding->Offset + attrib->RelativeOffset);
308 }
309 else
310 ptr = attrib->Ptr;
311
312 /* Just make sure the array is floating point, otherwise convert to
313 * temporary storage.
314 *
315 * XXX: remove the GLvector4f type at some stage and just use
316 * client arrays.
317 */
318 _tnl_import_array(ctx, i, count, binding, attrib, ptr);
319 }
320
321 /* We process only the vertices between min & max index:
322 */
323 VB->Count = count;
324
325 /* These should perhaps be part of _TNL_ATTRIB_* */
326 VB->BackfaceColorPtr = NULL;
327 VB->BackfaceIndexPtr = NULL;
328 VB->BackfaceSecondaryColorPtr = NULL;
329
330 /* Clipping and drawing code still requires this to be a packed
331 * array of ubytes which can be written into. TODO: Fix and
332 * remove.
333 */
334 if (ctx->Polygon.FrontMode != GL_FILL ||
335 ctx->Polygon.BackMode != GL_FILL)
336 {
337 VB->EdgeFlag = _tnl_import_edgeflag( ctx,
338 VB->AttribPtr[_TNL_ATTRIB_EDGEFLAG],
339 VB->Count );
340 }
341 else {
342 /* the data previously pointed to by EdgeFlag may have been freed */
343 VB->EdgeFlag = NULL;
344 }
345 }
346
347
348 /* Translate indices to GLuints and store in VB->Elts.
349 */
350 static void bind_indices( struct gl_context *ctx,
351 const struct _mesa_index_buffer *ib,
352 struct gl_buffer_object **bo,
353 GLuint *nr_bo)
354 {
355 TNLcontext *tnl = TNL_CONTEXT(ctx);
356 struct vertex_buffer *VB = &tnl->vb;
357 GLuint i;
358 const void *ptr;
359
360 if (!ib) {
361 VB->Elts = NULL;
362 return;
363 }
364
365 if (_mesa_is_bufferobj(ib->obj) &&
366 !_mesa_bufferobj_mapped(ib->obj, MAP_INTERNAL)) {
367 /* if the buffer object isn't mapped yet, map it now */
368 bo[*nr_bo] = ib->obj;
369 (*nr_bo)++;
370 ptr = ctx->Driver.MapBufferRange(ctx, (GLsizeiptr) ib->ptr,
371 ib->count * ib->index_size,
372 GL_MAP_READ_BIT, ib->obj,
373 MAP_INTERNAL);
374 assert(ib->obj->Mappings[MAP_INTERNAL].Pointer);
375 } else {
376 /* user-space elements, or buffer already mapped */
377 ptr = ADD_POINTERS(ib->obj->Mappings[MAP_INTERNAL].Pointer, ib->ptr);
378 }
379
380 if (ib->index_size == 4 && VB->Primitive[0].basevertex == 0) {
381 VB->Elts = (GLuint *) ptr;
382 }
383 else {
384 GLuint *elts = (GLuint *)get_space(ctx, ib->count * sizeof(GLuint));
385 VB->Elts = elts;
386
387 if (ib->index_size == 4) {
388 const GLuint *in = (GLuint *)ptr;
389 for (i = 0; i < ib->count; i++)
390 *elts++ = (GLuint)(*in++) + VB->Primitive[0].basevertex;
391 }
392 else if (ib->index_size == 2) {
393 const GLushort *in = (GLushort *)ptr;
394 for (i = 0; i < ib->count; i++)
395 *elts++ = (GLuint)(*in++) + VB->Primitive[0].basevertex;
396 }
397 else {
398 const GLubyte *in = (GLubyte *)ptr;
399 for (i = 0; i < ib->count; i++)
400 *elts++ = (GLuint)(*in++) + VB->Primitive[0].basevertex;
401 }
402 }
403 }
404
405 static void bind_prims( struct gl_context *ctx,
406 const struct _mesa_prim *prim,
407 GLuint nr_prims )
408 {
409 TNLcontext *tnl = TNL_CONTEXT(ctx);
410 struct vertex_buffer *VB = &tnl->vb;
411
412 VB->Primitive = prim;
413 VB->PrimitiveCount = nr_prims;
414 }
415
416 static void unmap_vbos( struct gl_context *ctx,
417 struct gl_buffer_object **bo,
418 GLuint nr_bo )
419 {
420 GLuint i;
421 for (i = 0; i < nr_bo; i++) {
422 ctx->Driver.UnmapBuffer(ctx, bo[i], MAP_INTERNAL);
423 }
424 }
425
426
427 /* This is the main workhorse doing all the rendering work.
428 */
429 void _tnl_draw_prims(struct gl_context *ctx,
430 const struct tnl_vertex_array *arrays,
431 const struct _mesa_prim *prim,
432 GLuint nr_prims,
433 const struct _mesa_index_buffer *ib,
434 GLboolean index_bounds_valid,
435 GLuint min_index,
436 GLuint max_index,
437 struct gl_transform_feedback_object *tfb_vertcount,
438 unsigned stream,
439 struct gl_buffer_object *indirect)
440 {
441 TNLcontext *tnl = TNL_CONTEXT(ctx);
442 const GLuint TEST_SPLIT = 0;
443 const GLint max = TEST_SPLIT ? 8 : tnl->vb.Size - MAX_CLIPPED_VERTICES;
444 GLint max_basevertex = prim->basevertex;
445 GLuint i;
446
447 if (!index_bounds_valid)
448 vbo_get_minmax_indices(ctx, prim, ib, &min_index, &max_index, nr_prims);
449
450 /* Mesa core state should have been validated already */
451 assert(ctx->NewState == 0x0);
452
453 if (!_mesa_check_conditional_render(ctx))
454 return; /* don't draw */
455
456 for (i = 1; i < nr_prims; i++)
457 max_basevertex = MAX2(max_basevertex, prim[i].basevertex);
458
459 if (0)
460 {
461 printf("%s %d..%d\n", __func__, min_index, max_index);
462 for (i = 0; i < nr_prims; i++)
463 printf("prim %d: %s start %d count %d\n", i,
464 _mesa_enum_to_string(prim[i].mode),
465 prim[i].start,
466 prim[i].count);
467 }
468
469 if (min_index) {
470 /* We always translate away calls with min_index != 0.
471 */
472 t_rebase_prims( ctx, arrays, prim, nr_prims, ib,
473 min_index, max_index,
474 _tnl_draw_prims );
475 return;
476 }
477 else if ((GLint)max_index + max_basevertex > max) {
478 /* The software TNL pipeline has a fixed amount of storage for
479 * vertices and it is necessary to split incoming drawing commands
480 * if they exceed that limit.
481 */
482 struct split_limits limits;
483 limits.max_verts = max;
484 limits.max_vb_size = ~0;
485 limits.max_indices = ~0;
486
487 /* This will split the buffers one way or another and
488 * recursively call back into this function.
489 */
490 _tnl_split_prims( ctx, arrays, prim, nr_prims, ib,
491 0, max_index + prim->basevertex,
492 _tnl_draw_prims,
493 &limits );
494 }
495 else {
496 /* May need to map a vertex buffer object for every attribute plus
497 * one for the index buffer.
498 */
499 struct gl_buffer_object *bo[VERT_ATTRIB_MAX + 1];
500 GLuint nr_bo = 0;
501 GLuint inst;
502
503 for (i = 0; i < nr_prims;) {
504 GLuint this_nr_prims;
505
506 /* Our SW TNL pipeline doesn't handle basevertex yet, so bind_indices
507 * will rebase the elements to the basevertex, and we'll only
508 * emit strings of prims with the same basevertex in one draw call.
509 */
510 for (this_nr_prims = 1; i + this_nr_prims < nr_prims;
511 this_nr_prims++) {
512 if (prim[i].basevertex != prim[i + this_nr_prims].basevertex)
513 break;
514 }
515
516 assert(prim[i].num_instances > 0);
517
518 /* Binding inputs may imply mapping some vertex buffer objects.
519 * They will need to be unmapped below.
520 */
521 for (inst = 0; inst < prim[i].num_instances; inst++) {
522
523 bind_prims(ctx, &prim[i], this_nr_prims);
524 bind_inputs(ctx, arrays, max_index + prim[i].basevertex + 1,
525 bo, &nr_bo);
526 bind_indices(ctx, ib, bo, &nr_bo);
527
528 tnl->CurInstance = inst;
529 TNL_CONTEXT(ctx)->Driver.RunPipeline(ctx);
530
531 unmap_vbos(ctx, bo, nr_bo);
532 free_space(ctx);
533 }
534
535 i += this_nr_prims;
536 }
537 }
538 }
539
540
541 void
542 _tnl_init_inputs(struct tnl_inputs *inputs)
543 {
544 inputs->current = 0;
545 inputs->vertex_processing_mode = VP_MODE_FF;
546 }
547
548
549 /**
550 * Update the tnl_inputs's arrays to point to the vao->_VertexArray arrays
551 * according to the 'enable' bitmask.
552 * \param enable bitfield of VERT_BIT_x flags.
553 */
554 static inline void
555 update_vao_inputs(struct gl_context *ctx,
556 struct tnl_inputs *inputs, GLbitfield enable)
557 {
558 const struct gl_vertex_array_object *vao = ctx->Array._DrawVAO;
559
560 /* Make sure we process only arrays enabled in the VAO */
561 assert((enable & ~_mesa_get_vao_vp_inputs(vao)) == 0);
562
563 /* Fill in the client arrays from the VAO */
564 const struct gl_vertex_buffer_binding *bindings = &vao->BufferBinding[0];
565 while (enable) {
566 const int attr = u_bit_scan(&enable);
567 struct tnl_vertex_array *input = &inputs->inputs[attr];
568 const struct gl_array_attributes *attrib;
569 attrib = _mesa_draw_array_attrib(vao, attr);
570 input->VertexAttrib = attrib;
571 input->BufferBinding = &bindings[attrib->BufferBindingIndex];
572 }
573 }
574
575
576 /**
577 * Update the tnl_inputs's arrays to point to the vbo->currval arrays
578 * according to the 'current' bitmask.
579 * \param current bitfield of VERT_BIT_x flags.
580 */
581 static inline void
582 update_current_inputs(struct gl_context *ctx,
583 struct tnl_inputs *inputs, GLbitfield current)
584 {
585 gl_vertex_processing_mode mode = ctx->VertexProgram._VPMode;
586
587 /* All previously non current array pointers need update. */
588 GLbitfield mask = current & ~inputs->current;
589 /* On mode change, the slots aliasing with materials need update too */
590 if (mode != inputs->vertex_processing_mode)
591 mask |= current & VERT_BIT_MAT_ALL;
592
593 while (mask) {
594 const int attr = u_bit_scan(&mask);
595 struct tnl_vertex_array *input = &inputs->inputs[attr];
596 input->VertexAttrib = _vbo_current_attrib(ctx, attr);
597 input->BufferBinding = _vbo_current_binding(ctx);
598 }
599
600 inputs->current = current;
601 inputs->vertex_processing_mode = mode;
602 }
603
604
605 /**
606 * Update the tnl_inputs's arrays to point to the vao->_VertexArray and
607 * vbo->currval arrays according to Array._DrawVAO and
608 * Array._DrawVAOEnableAttribs.
609 */
610 void
611 _tnl_update_inputs(struct gl_context *ctx, struct tnl_inputs *inputs)
612 {
613 const GLbitfield enable = ctx->Array._DrawVAOEnabledAttribs;
614
615 /* Update array input pointers */
616 update_vao_inputs(ctx, inputs, enable);
617
618 /* The rest must be current inputs. */
619 update_current_inputs(ctx, inputs, ~enable & VERT_BIT_ALL);
620 }
621
622
623 const struct tnl_vertex_array*
624 _tnl_bind_inputs( struct gl_context *ctx )
625 {
626 TNLcontext *tnl = TNL_CONTEXT(ctx);
627 _tnl_update_inputs(ctx, &tnl->draw_arrays);
628 return tnl->draw_arrays.inputs;
629 }
630
631
632 /* This is the main entrypoint into the slimmed-down software tnl
633 * module. In a regular swtnl driver, this can be plugged straight
634 * into the ctx->Driver.Draw() callback.
635 */
636 void
637 _tnl_draw(struct gl_context *ctx,
638 const struct _mesa_prim *prim, GLuint nr_prims,
639 const struct _mesa_index_buffer *ib,
640 GLboolean index_bounds_valid, GLuint min_index, GLuint max_index,
641 struct gl_transform_feedback_object *tfb_vertcount,
642 unsigned stream, struct gl_buffer_object *indirect)
643 {
644 /* Update TNLcontext::draw_arrays and return that pointer.
645 */
646 const struct tnl_vertex_array* arrays = _tnl_bind_inputs(ctx);
647
648 _tnl_draw_prims(ctx, arrays, prim, nr_prims, ib,
649 index_bounds_valid, min_index, max_index,
650 tfb_vertcount, stream, indirect);
651 }
652
653
654 void
655 _tnl_init_driver_draw_function(struct dd_function_table *functions)
656 {
657 functions->Draw = _tnl_draw;
658 }