remove final imports.h and imports.c bits
[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
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 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
61 for (GLuint i = 0; i < tnl->nr_blocks; i++)
62 free(tnl->block[i]);
63
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->Format.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->Format.Normalized);
108 assert(attrib->Format.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 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->Format.Size;
155
156 if (attrib->Format.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->Format.Type != GL_FLOAT) {
190 const GLuint sz = attrib->Format.Size;
191 GLubyte *buf = get_space(ctx, count * sz * sizeof(GLfloat));
192 GLfloat *fptr = (GLfloat *)buf;
193
194 switch (attrib->Format.Type) {
195 case GL_BYTE:
196 CONVERT(GLbyte, BYTE_TO_FLOAT);
197 break;
198 case GL_UNSIGNED_BYTE:
199 if (attrib->Format.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, 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 unreachable("Invalid type.");
230 }
231
232 ptr = buf;
233 stride = sz * sizeof(GLfloat);
234 }
235
236 VB->AttribPtr[attr] = &tnl->tmp_inputs[attr];
237 VB->AttribPtr[attr]->data = (GLfloat (*)[4])ptr;
238 VB->AttribPtr[attr]->start = (GLfloat *)ptr;
239 VB->AttribPtr[attr]->count = count;
240 VB->AttribPtr[attr]->stride = stride;
241 VB->AttribPtr[attr]->size = attrib->Format.Size;
242
243 /* This should die, but so should the whole GLvector4f concept:
244 */
245 VB->AttribPtr[attr]->flags = (((1<<attrib->Format.Size)-1) |
246 VEC_NOT_WRITEABLE |
247 (stride == 4*sizeof(GLfloat) ? 0 : VEC_BAD_STRIDE));
248
249 VB->AttribPtr[attr]->storage = NULL;
250 }
251
252 #define CLIPVERTS ((6 + MAX_CLIP_PLANES) * 2)
253
254
255 static GLboolean *_tnl_import_edgeflag(struct gl_context *ctx,
256 const GLvector4f *input,
257 GLuint count)
258 {
259 const GLubyte *ptr = (const GLubyte *)input->data;
260 const GLuint stride = input->stride;
261 GLboolean *space = (GLboolean *)get_space(ctx, count + CLIPVERTS);
262 GLboolean *bptr = space;
263
264 for (GLuint i = 0; i < count; i++) {
265 *bptr++ = ((GLfloat *)ptr)[0] == 1.0F;
266 ptr += stride;
267 }
268
269 return space;
270 }
271
272
273 static void bind_inputs(struct gl_context *ctx,
274 const struct tnl_vertex_array *inputs,
275 GLint count,
276 struct gl_buffer_object **bo,
277 GLuint *nr_bo)
278 {
279 TNLcontext *tnl = TNL_CONTEXT(ctx);
280 struct vertex_buffer *VB = &tnl->vb;
281
282 /* Map all the VBOs
283 */
284 for (unsigned i = 0; i < VERT_ATTRIB_MAX; i++) {
285 const struct tnl_vertex_array *array = &inputs[i];
286 const struct gl_vertex_buffer_binding *binding = array->BufferBinding;
287 const struct gl_array_attributes *attrib = array->VertexAttrib;
288 const void *ptr;
289
290 if (binding->BufferObj) {
291 if (!binding->BufferObj->Mappings[MAP_INTERNAL].Pointer) {
292 bo[*nr_bo] = binding->BufferObj;
293 (*nr_bo)++;
294 ctx->Driver.MapBufferRange(ctx, 0, binding->BufferObj->Size,
295 GL_MAP_READ_BIT,
296 binding->BufferObj,
297 MAP_INTERNAL);
298
299 assert(binding->BufferObj->Mappings[MAP_INTERNAL].Pointer);
300 }
301
302 ptr = ADD_POINTERS(binding->BufferObj->Mappings[MAP_INTERNAL].Pointer,
303 binding->Offset + attrib->RelativeOffset);
304 } else
305 ptr = attrib->Ptr;
306
307 /* Just make sure the array is floating point, otherwise convert to
308 * temporary storage.
309 *
310 * XXX: remove the GLvector4f type at some stage and just use
311 * client arrays.
312 */
313 _tnl_import_array(ctx, i, count, binding, attrib, ptr);
314 }
315
316 /* We process only the vertices between min & max index:
317 */
318 VB->Count = count;
319
320 /* These should perhaps be part of _TNL_ATTRIB_* */
321 VB->BackfaceColorPtr = NULL;
322 VB->BackfaceIndexPtr = NULL;
323 VB->BackfaceSecondaryColorPtr = NULL;
324
325 /* Clipping and drawing code still requires this to be a packed
326 * array of ubytes which can be written into. TODO: Fix and
327 * remove.
328 */
329 if (ctx->Polygon.FrontMode != GL_FILL ||
330 ctx->Polygon.BackMode != GL_FILL) {
331 VB->EdgeFlag = _tnl_import_edgeflag(ctx,
332 VB->AttribPtr[_TNL_ATTRIB_EDGEFLAG],
333 VB->Count);
334 } else {
335 /* the data previously pointed to by EdgeFlag may have been freed */
336 VB->EdgeFlag = NULL;
337 }
338 }
339
340
341 /* Translate indices to GLuints and store in VB->Elts.
342 */
343 static void bind_indices(struct gl_context *ctx,
344 const struct _mesa_index_buffer *ib,
345 struct gl_buffer_object **bo,
346 GLuint *nr_bo)
347 {
348 TNLcontext *tnl = TNL_CONTEXT(ctx);
349 struct vertex_buffer *VB = &tnl->vb;
350 GLuint i;
351 const void *ptr;
352
353 if (!ib) {
354 VB->Elts = NULL;
355 return;
356 }
357
358 if (ib->obj) {
359 if (!_mesa_bufferobj_mapped(ib->obj, MAP_INTERNAL)) {
360 /* if the buffer object isn't mapped yet, map it now */
361 bo[*nr_bo] = ib->obj;
362 (*nr_bo)++;
363 ptr = ctx->Driver.MapBufferRange(ctx, (GLsizeiptr) ib->ptr,
364 ib->count << ib->index_size_shift,
365 GL_MAP_READ_BIT, ib->obj,
366 MAP_INTERNAL);
367 assert(ib->obj->Mappings[MAP_INTERNAL].Pointer);
368 } else {
369 /* user-space elements, or buffer already mapped */
370 ptr = ADD_POINTERS(ib->obj->Mappings[MAP_INTERNAL].Pointer, ib->ptr);
371 }
372 } else
373 ptr = ib->ptr;
374
375 if (ib->index_size_shift == 2 && VB->Primitive[0].basevertex == 0) {
376 VB->Elts = (GLuint *) ptr;
377 }
378 else {
379 GLuint *elts = (GLuint *)get_space(ctx, ib->count * sizeof(GLuint));
380 VB->Elts = elts;
381
382 if (ib->index_size_shift == 2) {
383 const GLuint *in = (GLuint *)ptr;
384 for (i = 0; i < ib->count; i++)
385 *elts++ = (GLuint)(*in++) + VB->Primitive[0].basevertex;
386 }
387 else if (ib->index_size_shift == 1) {
388 const GLushort *in = (GLushort *)ptr;
389 for (i = 0; i < ib->count; i++)
390 *elts++ = (GLuint)(*in++) + VB->Primitive[0].basevertex;
391 }
392 else {
393 const GLubyte *in = (GLubyte *)ptr;
394 for (i = 0; i < ib->count; i++)
395 *elts++ = (GLuint)(*in++) + VB->Primitive[0].basevertex;
396 }
397 }
398 }
399
400 static void bind_prims(struct gl_context *ctx,
401 const struct _mesa_prim *prim,
402 GLuint nr_prims)
403 {
404 TNLcontext *tnl = TNL_CONTEXT(ctx);
405 struct vertex_buffer *VB = &tnl->vb;
406
407 VB->Primitive = prim;
408 VB->PrimitiveCount = nr_prims;
409 }
410
411 static void unmap_vbos(struct gl_context *ctx,
412 struct gl_buffer_object **bo,
413 GLuint nr_bo)
414 {
415 for (GLuint i = 0; i < nr_bo; i++) {
416 ctx->Driver.UnmapBuffer(ctx, bo[i], MAP_INTERNAL);
417 }
418 }
419
420
421 /* This is the main workhorse doing all the rendering work.
422 */
423 void _tnl_draw_prims(struct gl_context *ctx,
424 const struct tnl_vertex_array *arrays,
425 const struct _mesa_prim *prim,
426 GLuint nr_prims,
427 const struct _mesa_index_buffer *ib,
428 GLboolean index_bounds_valid,
429 GLuint min_index,
430 GLuint max_index,
431 GLuint num_instances,
432 GLuint base_instance)
433 {
434 TNLcontext *tnl = TNL_CONTEXT(ctx);
435 const GLuint TEST_SPLIT = 0;
436 const GLint max = TEST_SPLIT ? 8 : tnl->vb.Size - MAX_CLIPPED_VERTICES;
437 GLint max_basevertex = prim->basevertex;
438 GLuint i;
439
440 if (!index_bounds_valid)
441 vbo_get_minmax_indices(ctx, prim, ib, &min_index, &max_index, nr_prims);
442
443 /* Mesa core state should have been validated already */
444 assert(ctx->NewState == 0x0);
445
446 if (!_mesa_check_conditional_render(ctx))
447 return; /* don't draw */
448
449 for (i = 1; i < nr_prims; i++)
450 max_basevertex = MAX2(max_basevertex, prim[i].basevertex);
451
452 if (0) {
453 printf("%s %d..%d\n", __func__, min_index, max_index);
454 for (i = 0; i < nr_prims; i++)
455 printf("prim %d: %s start %d count %d\n", i,
456 _mesa_enum_to_string(prim[i].mode),
457 prim[i].start,
458 prim[i].count);
459 }
460
461 if (min_index) {
462 /* We always translate away calls with min_index != 0.
463 */
464 t_rebase_prims(ctx, arrays, prim, nr_prims, ib,
465 min_index, max_index, num_instances, base_instance,
466 _tnl_draw_prims);
467 return;
468 }
469 else if ((GLint)max_index + max_basevertex > max) {
470 /* The software TNL pipeline has a fixed amount of storage for
471 * vertices and it is necessary to split incoming drawing commands
472 * if they exceed that limit.
473 */
474 struct split_limits limits;
475 limits.max_verts = max;
476 limits.max_vb_size = ~0;
477 limits.max_indices = ~0;
478
479 /* This will split the buffers one way or another and
480 * recursively call back into this function.
481 */
482 _tnl_split_prims(ctx, arrays, prim, nr_prims, ib,
483 0, max_index + prim->basevertex,
484 num_instances, base_instance,
485 _tnl_draw_prims,
486 &limits);
487 }
488 else {
489 /* May need to map a vertex buffer object for every attribute plus
490 * one for the index buffer.
491 */
492 struct gl_buffer_object *bo[VERT_ATTRIB_MAX + 1];
493 GLuint nr_bo = 0;
494 GLuint inst;
495
496 assert(num_instances > 0);
497
498 for (i = 0; i < nr_prims;) {
499 GLuint this_nr_prims;
500
501 /* Our SW TNL pipeline doesn't handle basevertex yet, so bind_indices
502 * will rebase the elements to the basevertex, and we'll only
503 * emit strings of prims with the same basevertex in one draw call.
504 */
505 for (this_nr_prims = 1; i + this_nr_prims < nr_prims;
506 this_nr_prims++) {
507 if (prim[i].basevertex != prim[i + this_nr_prims].basevertex)
508 break;
509 }
510
511 /* Binding inputs may imply mapping some vertex buffer objects.
512 * They will need to be unmapped below.
513 */
514 for (inst = 0; inst < num_instances; inst++) {
515
516 bind_prims(ctx, &prim[i], this_nr_prims);
517 bind_inputs(ctx, arrays, max_index + prim[i].basevertex + 1,
518 bo, &nr_bo);
519 bind_indices(ctx, ib, bo, &nr_bo);
520
521 tnl->CurInstance = inst;
522 TNL_CONTEXT(ctx)->Driver.RunPipeline(ctx);
523
524 unmap_vbos(ctx, bo, nr_bo);
525 free_space(ctx);
526 }
527
528 i += this_nr_prims;
529 }
530 }
531 }
532
533
534 void
535 _tnl_init_inputs(struct tnl_inputs *inputs)
536 {
537 inputs->current = 0;
538 inputs->vertex_processing_mode = VP_MODE_FF;
539 }
540
541
542 /**
543 * Update the tnl_inputs's arrays to point to the vao->_VertexArray arrays
544 * according to the 'enable' bitmask.
545 * \param enable bitfield of VERT_BIT_x flags.
546 */
547 static inline void
548 update_vao_inputs(struct gl_context *ctx,
549 struct tnl_inputs *inputs, GLbitfield enable)
550 {
551 const struct gl_vertex_array_object *vao = ctx->Array._DrawVAO;
552
553 /* Make sure we process only arrays enabled in the VAO */
554 assert((enable & ~_mesa_get_vao_vp_inputs(vao)) == 0);
555
556 /* Fill in the client arrays from the VAO */
557 const struct gl_vertex_buffer_binding *bindings = &vao->BufferBinding[0];
558 while (enable) {
559 const int attr = u_bit_scan(&enable);
560 struct tnl_vertex_array *input = &inputs->inputs[attr];
561 const struct gl_array_attributes *attrib;
562 attrib = _mesa_draw_array_attrib(vao, attr);
563 input->VertexAttrib = attrib;
564 input->BufferBinding = &bindings[attrib->BufferBindingIndex];
565 }
566 }
567
568
569 /**
570 * Update the tnl_inputs's arrays to point to the vbo->currval arrays
571 * according to the 'current' bitmask.
572 * \param current bitfield of VERT_BIT_x flags.
573 */
574 static inline void
575 update_current_inputs(struct gl_context *ctx,
576 struct tnl_inputs *inputs, GLbitfield current)
577 {
578 gl_vertex_processing_mode mode = ctx->VertexProgram._VPMode;
579
580 /* All previously non current array pointers need update. */
581 GLbitfield mask = current & ~inputs->current;
582 /* On mode change, the slots aliasing with materials need update too */
583 if (mode != inputs->vertex_processing_mode)
584 mask |= current & VERT_BIT_MAT_ALL;
585
586 while (mask) {
587 const int attr = u_bit_scan(&mask);
588 struct tnl_vertex_array *input = &inputs->inputs[attr];
589 input->VertexAttrib = _vbo_current_attrib(ctx, attr);
590 input->BufferBinding = _vbo_current_binding(ctx);
591 }
592
593 inputs->current = current;
594 inputs->vertex_processing_mode = mode;
595 }
596
597
598 /**
599 * Update the tnl_inputs's arrays to point to the vao->_VertexArray and
600 * vbo->currval arrays according to Array._DrawVAO and
601 * Array._DrawVAOEnableAttribs.
602 */
603 void
604 _tnl_update_inputs(struct gl_context *ctx, struct tnl_inputs *inputs)
605 {
606 const GLbitfield enable = ctx->Array._DrawVAOEnabledAttribs;
607
608 /* Update array input pointers */
609 update_vao_inputs(ctx, inputs, enable);
610
611 /* The rest must be current inputs. */
612 update_current_inputs(ctx, inputs, ~enable & VERT_BIT_ALL);
613 }
614
615
616 const struct tnl_vertex_array *
617 _tnl_bind_inputs(struct gl_context *ctx)
618 {
619 TNLcontext *tnl = TNL_CONTEXT(ctx);
620 _tnl_update_inputs(ctx, &tnl->draw_arrays);
621 return tnl->draw_arrays.inputs;
622 }
623
624
625 /* This is the main entrypoint into the slimmed-down software tnl
626 * module. In a regular swtnl driver, this can be plugged straight
627 * into the ctx->Driver.Draw() callback.
628 */
629 void
630 _tnl_draw(struct gl_context *ctx,
631 const struct _mesa_prim *prim, GLuint nr_prims,
632 const struct _mesa_index_buffer *ib,
633 GLboolean index_bounds_valid, GLuint min_index, GLuint max_index,
634 GLuint num_instances, GLuint base_instance,
635 UNUSED struct gl_transform_feedback_object *tfb_vertcount,
636 UNUSED unsigned stream)
637 {
638 /* Update TNLcontext::draw_arrays and return that pointer.
639 */
640 const struct tnl_vertex_array* arrays = _tnl_bind_inputs(ctx);
641
642 _tnl_draw_prims(ctx, arrays, prim, nr_prims, ib,
643 index_bounds_valid, min_index, max_index,
644 num_instances, base_instance);
645 }
646
647
648 void
649 _tnl_init_driver_draw_function(struct dd_function_table *functions)
650 {
651 functions->Draw = _tnl_draw;
652 }