c97cf5f7b21c30358547188cd550c7eb4e09a1bc
[mesa.git] / src / mesa / tnl / t_draw.c
1
2 /*
3 * Mesa 3-D graphics library
4 * Version: 6.5
5 *
6 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Keith Whitwell <keith@tungstengraphics.com>
27 */
28
29 #include "glheader.h"
30 #include "context.h"
31 #include "imports.h"
32 #include "state.h"
33 #include "mtypes.h"
34 #include "macros.h"
35 #include "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 /* Adjust pointer to point at first requested element, convert to
94 * floating point, populate VB->AttribPtr[].
95 */
96 static void _tnl_import_array( GLcontext *ctx,
97 GLuint attrib,
98 GLuint count,
99 const struct gl_client_array *input,
100 const GLubyte *ptr )
101 {
102 TNLcontext *tnl = TNL_CONTEXT(ctx);
103 struct vertex_buffer *VB = &tnl->vb;
104 GLuint stride = input->StrideB;
105
106 if (input->Type != GL_FLOAT) {
107 const GLuint sz = input->Size;
108 GLubyte *buf = get_space(ctx, count * sz * sizeof(GLfloat));
109 GLfloat *fptr = (GLfloat *)buf;
110
111 switch (input->Type) {
112 case GL_BYTE:
113 CONVERT(GLbyte, BYTE_TO_FLOAT);
114 break;
115 case GL_UNSIGNED_BYTE:
116 CONVERT(GLubyte, UBYTE_TO_FLOAT);
117 break;
118 case GL_SHORT:
119 CONVERT(GLshort, SHORT_TO_FLOAT);
120 break;
121 case GL_UNSIGNED_SHORT:
122 CONVERT(GLushort, USHORT_TO_FLOAT);
123 break;
124 case GL_INT:
125 CONVERT(GLint, INT_TO_FLOAT);
126 break;
127 case GL_UNSIGNED_INT:
128 CONVERT(GLuint, UINT_TO_FLOAT);
129 break;
130 case GL_DOUBLE:
131 CONVERT(GLdouble, (GLfloat));
132 break;
133 default:
134 assert(0);
135 break;
136 }
137
138 ptr = buf;
139 stride = sz * sizeof(GLfloat);
140 }
141
142 VB->AttribPtr[attrib] = &tnl->tmp_inputs[attrib];
143 VB->AttribPtr[attrib]->data = (GLfloat (*)[4])ptr;
144 VB->AttribPtr[attrib]->start = (GLfloat *)ptr;
145 VB->AttribPtr[attrib]->count = count;
146 VB->AttribPtr[attrib]->stride = stride;
147 VB->AttribPtr[attrib]->size = input->Size;
148
149 /* This should die, but so should the whole GLvector4f concept:
150 */
151 VB->AttribPtr[attrib]->flags = (((1<<input->Size)-1) |
152 VEC_NOT_WRITEABLE |
153 (stride == 4*sizeof(GLfloat) ? 0 : VEC_BAD_STRIDE));
154
155 VB->AttribPtr[attrib]->storage = NULL;
156 }
157
158 #define CLIPVERTS ((6 + MAX_CLIP_PLANES) * 2)
159
160
161 static GLboolean *_tnl_import_edgeflag( GLcontext *ctx,
162 const GLvector4f *input,
163 GLuint count)
164 {
165 const GLubyte *ptr = (const GLubyte *)input->data;
166 const GLuint stride = input->stride;
167 GLboolean *space = (GLboolean *)get_space(ctx, count + CLIPVERTS);
168 GLboolean *bptr = space;
169 GLuint i;
170
171 for (i = 0; i < count; i++) {
172 *bptr++ = ((GLfloat *)ptr)[0] == 1.0;
173 ptr += stride;
174 }
175
176 return space;
177 }
178
179
180 static void bind_inputs( GLcontext *ctx,
181 const struct gl_client_array *inputs[],
182 GLint count,
183 struct gl_buffer_object **bo,
184 GLuint *nr_bo )
185 {
186 TNLcontext *tnl = TNL_CONTEXT(ctx);
187 struct vertex_buffer *VB = &tnl->vb;
188 GLuint i;
189
190 /* Map all the VBOs
191 */
192 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
193 const void *ptr;
194
195 if (inputs[i]->BufferObj->Name) {
196 if (!inputs[i]->BufferObj->Pointer) {
197 bo[*nr_bo] = inputs[i]->BufferObj;
198 (*nr_bo)++;
199 ctx->Driver.MapBuffer(ctx,
200 GL_ARRAY_BUFFER,
201 GL_READ_ONLY_ARB,
202 inputs[i]->BufferObj);
203
204 assert(inputs[i]->BufferObj->Pointer);
205 }
206
207 ptr = ADD_POINTERS(inputs[i]->BufferObj->Pointer,
208 inputs[i]->Ptr);
209 }
210 else
211 ptr = inputs[i]->Ptr;
212
213 /* Just make sure the array is floating point, otherwise convert to
214 * temporary storage.
215 *
216 * XXX: remove the GLvector4f type at some stage and just use
217 * client arrays.
218 */
219 _tnl_import_array(ctx, i, count, inputs[i], ptr);
220 }
221
222 /* We process only the vertices between min & max index:
223 */
224 VB->Count = count;
225
226
227 /* Legacy pointers -- remove one day.
228 */
229 VB->ObjPtr = VB->AttribPtr[_TNL_ATTRIB_POS];
230 VB->NormalPtr = VB->AttribPtr[_TNL_ATTRIB_NORMAL];
231 VB->ColorPtr[0] = VB->AttribPtr[_TNL_ATTRIB_COLOR0];
232 VB->ColorPtr[1] = NULL;
233 VB->IndexPtr[0] = VB->AttribPtr[_TNL_ATTRIB_COLOR_INDEX];
234 VB->IndexPtr[1] = NULL;
235 VB->SecondaryColorPtr[0] = VB->AttribPtr[_TNL_ATTRIB_COLOR1];
236 VB->SecondaryColorPtr[1] = NULL;
237 VB->FogCoordPtr = VB->AttribPtr[_TNL_ATTRIB_FOG];
238
239 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
240 VB->TexCoordPtr[i] = VB->AttribPtr[_TNL_ATTRIB_TEX0 + i];
241 }
242
243 /* Clipping and drawing code still requires this to be a packed
244 * array of ubytes which can be written into. TODO: Fix and
245 * remove.
246 */
247 if (ctx->Polygon.FrontMode != GL_FILL ||
248 ctx->Polygon.BackMode != GL_FILL)
249 {
250 VB->EdgeFlag = _tnl_import_edgeflag( ctx,
251 VB->AttribPtr[_TNL_ATTRIB_EDGEFLAG],
252 VB->Count );
253 }
254
255 }
256
257
258 /* Translate indices to GLuints and store in VB->Elts.
259 */
260 static void bind_indices( GLcontext *ctx,
261 const struct _mesa_index_buffer *ib,
262 struct gl_buffer_object **bo,
263 GLuint *nr_bo)
264 {
265 TNLcontext *tnl = TNL_CONTEXT(ctx);
266 struct vertex_buffer *VB = &tnl->vb;
267 GLuint i;
268 void *ptr;
269
270 if (!ib) {
271 VB->Elts = NULL;
272 return;
273 }
274
275 if (ib->obj->Name && !ib->obj->Pointer) {
276 bo[*nr_bo] = ib->obj;
277 (*nr_bo)++;
278 ctx->Driver.MapBuffer(ctx,
279 GL_ELEMENT_ARRAY_BUFFER,
280 GL_READ_ONLY_ARB,
281 ib->obj);
282
283 assert(ib->obj->Pointer);
284 }
285
286 ptr = ADD_POINTERS(ib->obj->Pointer, ib->ptr);
287
288 if (ib->type == GL_UNSIGNED_INT) {
289 VB->Elts = (GLuint *) ptr;
290 }
291 else {
292 GLuint *elts = (GLuint *)get_space(ctx, ib->count * sizeof(GLuint));
293 VB->Elts = elts;
294
295 if (ib->type == GL_UNSIGNED_SHORT) {
296 const GLushort *in = (GLushort *)ptr;
297 for (i = 0; i < ib->count; i++)
298 *elts++ = (GLuint)(*in++);
299 }
300 else {
301 const GLubyte *in = (GLubyte *)ptr;
302 for (i = 0; i < ib->count; i++)
303 *elts++ = (GLuint)(*in++);
304 }
305 }
306 }
307
308 static void bind_prims( GLcontext *ctx,
309 const struct _mesa_prim *prim,
310 GLuint nr_prims )
311 {
312 TNLcontext *tnl = TNL_CONTEXT(ctx);
313 struct vertex_buffer *VB = &tnl->vb;
314
315 VB->Primitive = prim;
316 VB->PrimitiveCount = nr_prims;
317 }
318
319 static void unmap_vbos( GLcontext *ctx,
320 struct gl_buffer_object **bo,
321 GLuint nr_bo )
322 {
323 GLuint i;
324 for (i = 0; i < nr_bo; i++) {
325 ctx->Driver.UnmapBuffer(ctx,
326 0, /* target -- I don't see why this would be needed */
327 bo[i]);
328 }
329 }
330
331
332
333 /* This is the main entrypoint into the slimmed-down software tnl
334 * module. In a regular swtnl driver, this can be plugged straight
335 * into the vbo->Driver.DrawPrims() callback.
336 */
337 void _tnl_draw_prims( GLcontext *ctx,
338 const struct gl_client_array *arrays[],
339 const struct _mesa_prim *prim,
340 GLuint nr_prims,
341 const struct _mesa_index_buffer *ib,
342 GLuint min_index,
343 GLuint max_index)
344 {
345 TNLcontext *tnl = TNL_CONTEXT(ctx);
346 const GLuint TEST_SPLIT = 0;
347 const GLint max = TEST_SPLIT ? 8 : tnl->vb.Size - MAX_CLIPPED_VERTICES;
348
349 if (0)
350 {
351 GLuint i;
352 _mesa_printf("%s %d..%d\n", __FUNCTION__, min_index, max_index);
353 for (i = 0; i < nr_prims; i++)
354 _mesa_printf("prim %d: %s start %d count %d\n", i,
355 _mesa_lookup_enum_by_nr(prim[i].mode),
356 prim[i].start,
357 prim[i].count);
358 }
359
360 if (min_index) {
361 /* We always translate away calls with min_index != 0.
362 */
363 vbo_rebase_prims( ctx, arrays, prim, nr_prims, ib,
364 min_index, max_index,
365 _tnl_draw_prims );
366 return;
367 }
368 else if (max_index >= max) {
369 /* The software TNL pipeline has a fixed amount of storage for
370 * vertices and it is necessary to split incoming drawing commands
371 * if they exceed that limit.
372 */
373 struct split_limits limits;
374 limits.max_verts = max;
375 limits.max_vb_size = ~0;
376 limits.max_indices = ~0;
377
378 /* This will split the buffers one way or another and
379 * recursively call back into this function.
380 */
381 vbo_split_prims( ctx, arrays, prim, nr_prims, ib,
382 0, max_index,
383 _tnl_draw_prims,
384 &limits );
385 }
386 else {
387 /* May need to map a vertex buffer object for every attribute plus
388 * one for the index buffer.
389 */
390 struct gl_buffer_object *bo[VERT_ATTRIB_MAX + 1];
391 GLuint nr_bo = 0;
392
393 /* Binding inputs may imply mapping some vertex buffer objects.
394 * They will need to be unmapped below.
395 */
396 bind_inputs(ctx, arrays, max_index+1, bo, &nr_bo);
397 bind_indices(ctx, ib, bo, &nr_bo);
398 bind_prims(ctx, prim, nr_prims );
399
400 TNL_CONTEXT(ctx)->Driver.RunPipeline(ctx);
401
402 unmap_vbos(ctx, bo, nr_bo);
403 free_space(ctx);
404 }
405 }
406