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