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