OSDN Git Service

Merge branch 'mesa_7_5_branch'
[android-x86/external-mesa.git] / src / mesa / vbo / vbo_exec_array.c
1 /**************************************************************************
2  * 
3  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4  * 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
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  * 
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * 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
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  * 
26  **************************************************************************/
27
28 #include "main/glheader.h"
29 #include "main/context.h"
30 #include "main/state.h"
31 #include "main/api_validate.h"
32 #include "main/api_noop.h"
33 #include "main/varray.h"
34 #include "main/bufferobj.h"
35 #include "glapi/dispatch.h"
36
37 #include "vbo_context.h"
38
39 /**
40  * Compute min and max elements for glDraw[Range]Elements() calls.
41  */
42 static void get_minmax_index( GLuint count, GLuint type, 
43                               const GLvoid *indices,
44                               GLuint *min_index,
45                               GLuint *max_index)
46 {
47    GLuint i;
48
49    switch(type) {
50    case GL_UNSIGNED_INT: {
51       const GLuint *ui_indices = (const GLuint *)indices;
52       GLuint max_ui = ui_indices[count-1];
53       GLuint min_ui = ui_indices[0];
54       for (i = 0; i < count; i++) {
55          if (ui_indices[i] > max_ui) max_ui = ui_indices[i];
56          if (ui_indices[i] < min_ui) min_ui = ui_indices[i];
57       }
58       *min_index = min_ui;
59       *max_index = max_ui;
60       break;
61    }
62    case GL_UNSIGNED_SHORT: {
63       const GLushort *us_indices = (const GLushort *)indices;
64       GLuint max_us = us_indices[count-1];
65       GLuint min_us = us_indices[0];
66       for (i = 0; i < count; i++) {
67          if (us_indices[i] > max_us) max_us = us_indices[i];
68          if (us_indices[i] < min_us) min_us = us_indices[i];
69       }
70       *min_index = min_us;
71       *max_index = max_us;
72       break;
73    }
74    case GL_UNSIGNED_BYTE: {
75       const GLubyte *ub_indices = (const GLubyte *)indices;
76       GLuint max_ub = ub_indices[count-1];
77       GLuint min_ub = ub_indices[0];
78       for (i = 0; i < count; i++) {
79          if (ub_indices[i] > max_ub) max_ub = ub_indices[i];
80          if (ub_indices[i] < min_ub) min_ub = ub_indices[i];
81       }
82       *min_index = min_ub;
83       *max_index = max_ub;
84       break;
85    }
86    default:
87       assert(0);
88       break;
89    }
90 }
91
92
93 /**
94  * Just translate the arrayobj into a sane layout.
95  */
96 static void bind_array_obj( GLcontext *ctx )
97 {
98    struct vbo_context *vbo = vbo_context(ctx);
99    struct vbo_exec_context *exec = &vbo->exec;
100    struct gl_array_object *arrayObj = ctx->Array.ArrayObj;
101    GLuint i;
102
103    /* TODO: Fix the ArrayObj struct to keep legacy arrays in an array
104     * rather than as individual named arrays.  Then this function can
105     * go away.
106     */
107    exec->array.legacy_array[VERT_ATTRIB_POS] = &arrayObj->Vertex;
108    exec->array.legacy_array[VERT_ATTRIB_WEIGHT] = &arrayObj->Weight;
109    exec->array.legacy_array[VERT_ATTRIB_NORMAL] = &arrayObj->Normal;
110    exec->array.legacy_array[VERT_ATTRIB_COLOR0] = &arrayObj->Color;
111    exec->array.legacy_array[VERT_ATTRIB_COLOR1] = &arrayObj->SecondaryColor;
112    exec->array.legacy_array[VERT_ATTRIB_FOG] = &arrayObj->FogCoord;
113    exec->array.legacy_array[VERT_ATTRIB_COLOR_INDEX] = &arrayObj->Index;
114    if (arrayObj->PointSize.Enabled) {
115       /* this aliases COLOR_INDEX */
116       exec->array.legacy_array[VERT_ATTRIB_POINT_SIZE] = &arrayObj->PointSize;
117    }
118    exec->array.legacy_array[VERT_ATTRIB_EDGEFLAG] = &arrayObj->EdgeFlag;
119
120    for (i = 0; i < Elements(arrayObj->TexCoord); i++)
121       exec->array.legacy_array[VERT_ATTRIB_TEX0 + i] = &arrayObj->TexCoord[i];
122
123    for (i = 0; i < Elements(arrayObj->VertexAttrib); i++) {
124       assert(i < Elements(exec->array.generic_array));
125       exec->array.generic_array[i] = &arrayObj->VertexAttrib[i];
126    }
127    
128    exec->array.array_obj = arrayObj->Name;
129 }
130
131
132 static void recalculate_input_bindings( GLcontext *ctx )
133 {
134    struct vbo_context *vbo = vbo_context(ctx);
135    struct vbo_exec_context *exec = &vbo->exec;
136    const struct gl_client_array **inputs = &exec->array.inputs[0];
137    GLbitfield const_inputs = 0x0;
138    GLuint i;
139
140    exec->array.program_mode = get_program_mode(ctx);
141    exec->array.enabled_flags = ctx->Array.ArrayObj->_Enabled;
142
143    switch (exec->array.program_mode) {
144    case VP_NONE:
145       /* When no vertex program is active (or the vertex program is generated
146        * from fixed-function state).  We put the material values into the
147        * generic slots.  This is the only situation where material values
148        * are available as per-vertex attributes.
149        */
150       for (i = 0; i <= VERT_ATTRIB_TEX7; i++) {
151          if (exec->array.legacy_array[i]->Enabled)
152             inputs[i] = exec->array.legacy_array[i];
153          else {
154             inputs[i] = &vbo->legacy_currval[i];
155             const_inputs |= 1 << i;
156          }
157       }
158
159       for (i = 0; i < MAT_ATTRIB_MAX; i++) {
160          inputs[VERT_ATTRIB_GENERIC0 + i] = &vbo->mat_currval[i];
161          const_inputs |= 1 << (VERT_ATTRIB_GENERIC0 + i);
162       }
163
164       /* Could use just about anything, just to fill in the empty
165        * slots:
166        */
167       for (i = MAT_ATTRIB_MAX; i < VERT_ATTRIB_MAX - VERT_ATTRIB_GENERIC0; i++) {
168          inputs[VERT_ATTRIB_GENERIC0 + i] = &vbo->generic_currval[i];
169          const_inputs |= 1 << (VERT_ATTRIB_GENERIC0 + i);
170       }
171       break;
172
173    case VP_NV:
174       /* NV_vertex_program - attribute arrays alias and override
175        * conventional, legacy arrays.  No materials, and the generic
176        * slots are vacant.
177        */
178       for (i = 0; i <= VERT_ATTRIB_TEX7; i++) {
179          if (exec->array.generic_array[i]->Enabled)
180             inputs[i] = exec->array.generic_array[i];
181          else if (exec->array.legacy_array[i]->Enabled)
182             inputs[i] = exec->array.legacy_array[i];
183          else {
184             inputs[i] = &vbo->legacy_currval[i];
185             const_inputs |= 1 << i;
186          }
187       }
188
189       /* Could use just about anything, just to fill in the empty
190        * slots:
191        */
192       for (i = VERT_ATTRIB_GENERIC0; i < VERT_ATTRIB_MAX; i++) {
193          inputs[i] = &vbo->generic_currval[i - VERT_ATTRIB_GENERIC0];
194          const_inputs |= 1 << i;
195       }
196       break;
197
198    case VP_ARB:
199       /* GL_ARB_vertex_program or GLSL vertex shader - Only the generic[0]
200        * attribute array aliases and overrides the legacy position array.  
201        *
202        * Otherwise, legacy attributes available in the legacy slots,
203        * generic attributes in the generic slots and materials are not
204        * available as per-vertex attributes.
205        */
206       if (exec->array.generic_array[0]->Enabled)
207          inputs[0] = exec->array.generic_array[0];
208       else if (exec->array.legacy_array[0]->Enabled)
209          inputs[0] = exec->array.legacy_array[0];
210       else {
211          inputs[0] = &vbo->legacy_currval[0];
212          const_inputs |= 1 << 0;
213       }
214
215       for (i = 1; i <= VERT_ATTRIB_TEX7; i++) {
216          if (exec->array.legacy_array[i]->Enabled)
217             inputs[i] = exec->array.legacy_array[i];
218          else {
219             inputs[i] = &vbo->legacy_currval[i];
220             const_inputs |= 1 << i;
221          }
222       }
223
224       for (i = 0; i < MAX_VERTEX_GENERIC_ATTRIBS; i++) {
225          if (exec->array.generic_array[i]->Enabled)
226             inputs[VERT_ATTRIB_GENERIC0 + i] = exec->array.generic_array[i];
227          else {
228             inputs[VERT_ATTRIB_GENERIC0 + i] = &vbo->generic_currval[i];
229             const_inputs |= 1 << (VERT_ATTRIB_GENERIC0 + i);
230          }
231
232       }
233       break;
234    }
235
236    _mesa_set_varying_vp_inputs( ctx, ~const_inputs );
237 }
238
239
240 static void bind_arrays( GLcontext *ctx )
241 {
242 #if 0
243    if (ctx->Array.ArrayObj.Name != exec->array.array_obj) {
244       bind_array_obj(ctx);
245       recalculate_input_bindings(ctx);
246    }
247    else if (exec->array.program_mode != get_program_mode(ctx) ||
248             exec->array.enabled_flags != ctx->Array.ArrayObj->_Enabled) {
249       
250       recalculate_input_bindings(ctx);
251    }
252 #else
253    bind_array_obj(ctx);
254    recalculate_input_bindings(ctx);
255 #endif
256 }
257
258
259
260 /***********************************************************************
261  * API functions.
262  */
263
264 static void GLAPIENTRY
265 vbo_exec_DrawArrays(GLenum mode, GLint start, GLsizei count)
266 {
267    GET_CURRENT_CONTEXT(ctx);
268    struct vbo_context *vbo = vbo_context(ctx);
269    struct vbo_exec_context *exec = &vbo->exec;
270    struct _mesa_prim prim[1];
271
272    if (!_mesa_validate_DrawArrays( ctx, mode, start, count ))
273       return;
274
275    FLUSH_CURRENT( ctx, 0 );
276
277    if (ctx->NewState)
278       _mesa_update_state( ctx );
279       
280    if (!vbo_validate_shaders(ctx)) {
281       _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawArrays(bad shader)");
282       return;
283    }
284
285    bind_arrays( ctx );
286
287    /* Again... because we may have changed the bitmask of per-vertex varying
288     * attributes.  If we regenerate the fixed-function vertex program now
289     * we may be able to prune down the number of vertex attributes which we
290     * need in the shader.
291     */
292    if (ctx->NewState)
293       _mesa_update_state( ctx );
294
295    prim[0].begin = 1;
296    prim[0].end = 1;
297    prim[0].weak = 0;
298    prim[0].pad = 0;
299    prim[0].mode = mode;
300    prim[0].start = start;
301    prim[0].count = count;
302    prim[0].indexed = 0;
303
304    vbo->draw_prims( ctx, exec->array.inputs, prim, 1, NULL,
305                     start, start + count - 1 );
306
307 #if 0
308    {
309       int i;
310
311       _mesa_printf("vbo_exec_DrawArrays(mode 0x%x, start %d, count %d):\n",
312                    mode, start, count);
313
314       for (i = 0; i < 32; i++) {
315          GLuint bufName = exec->array.inputs[i]->BufferObj->Name;
316          GLint stride = exec->array.inputs[i]->Stride;
317          _mesa_printf("attr %2d: size %d stride %d  enabled %d  "
318                       "ptr %p  Bufobj %u\n",
319                       i,
320                       exec->array.inputs[i]->Size,
321                       stride,
322                       /*exec->array.inputs[i]->Enabled,*/
323                       exec->array.legacy_array[i]->Enabled,
324                       exec->array.inputs[i]->Ptr,
325                       bufName);
326          
327          if (bufName) {
328             struct gl_buffer_object *buf = _mesa_lookup_bufferobj(ctx, bufName);
329             GLubyte *p = ctx->Driver.MapBuffer(ctx, GL_ARRAY_BUFFER_ARB,
330                                             GL_READ_ONLY_ARB, buf);
331             int offset = (int) exec->array.inputs[i]->Ptr;
332             float *f = (float *) (p + offset);
333             int *k = (int *) f;
334             int i;
335             int n = (count * stride) / 4;
336             if (n > 32)
337                n = 32;
338             _mesa_printf("  Data at offset %d:\n", offset);
339             for (i = 0; i < n; i++) {
340                _mesa_printf("    float[%d] = 0x%08x %f\n", i, k[i], f[i]);
341             }
342             ctx->Driver.UnmapBuffer(ctx, GL_ARRAY_BUFFER_ARB, buf);
343          }
344       }
345    }
346 #endif
347 }
348
349
350 static void GLAPIENTRY
351 vbo_exec_DrawRangeElements(GLenum mode,
352                            GLuint start, GLuint end,
353                            GLsizei count, GLenum type, const GLvoid *indices)
354 {
355    GET_CURRENT_CONTEXT(ctx);
356    struct vbo_context *vbo = vbo_context(ctx);
357    struct vbo_exec_context *exec = &vbo->exec;
358    struct _mesa_index_buffer ib;
359    struct _mesa_prim prim[1];
360
361    if (!_mesa_validate_DrawRangeElements( ctx, mode, start, end, count, type, indices ))
362       return;
363
364    if (end >= ctx->Array.ArrayObj->_MaxElement) {
365       /* the max element is out of bounds of one or more enabled arrays */
366       _mesa_warning(ctx, "glDraw[Range]Elements(start %u, end %u, count %d, "
367                     "type 0x%x) index=%u is out of bounds (max=%u)",
368                     start, end, count, type, end,
369                     ctx->Array.ArrayObj->_MaxElement);
370       if (0)
371          _mesa_print_arrays(ctx);
372       return;
373    }
374
375    FLUSH_CURRENT( ctx, 0 );
376
377    if (ctx->NewState)
378       _mesa_update_state( ctx );
379
380    if (!vbo_validate_shaders(ctx)) {
381       _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawRangeElements(bad shader)");
382       return;
383    }
384
385    bind_arrays( ctx );
386
387    if (ctx->NewState)
388       _mesa_update_state( ctx );
389
390    ib.count = count;
391    ib.type = type; 
392    ib.obj = ctx->Array.ElementArrayBufferObj;
393    ib.ptr = indices;
394
395    prim[0].begin = 1;
396    prim[0].end = 1;
397    prim[0].weak = 0;
398    prim[0].pad = 0;
399    prim[0].mode = mode;
400    prim[0].start = 0;
401    prim[0].count = count;
402    prim[0].indexed = 1;
403
404    /* Need to give special consideration to rendering a range of
405     * indices starting somewhere above zero.  Typically the
406     * application is issuing multiple DrawRangeElements() to draw
407     * successive primitives layed out linearly in the vertex arrays.
408     * Unless the vertex arrays are all in a VBO (or locked as with
409     * CVA), the OpenGL semantics imply that we need to re-read or
410     * re-upload the vertex data on each draw call.  
411     *
412     * In the case of hardware tnl, we want to avoid starting the
413     * upload at zero, as it will mean every draw call uploads an
414     * increasing amount of not-used vertex data.  Worse - in the
415     * software tnl module, all those vertices might be transformed and
416     * lit but never rendered.
417     *
418     * If we just upload or transform the vertices in start..end,
419     * however, the indices will be incorrect.
420     *
421     * At this level, we don't know exactly what the requirements of
422     * the backend are going to be, though it will likely boil down to
423     * either:
424     *
425     * 1) Do nothing, everything is in a VBO and is processed once
426     *       only.
427     *
428     * 2) Adjust the indices and vertex arrays so that start becomes
429     *    zero.
430     *
431     * Rather than doing anything here, I'll provide a helper function
432     * for the latter case elsewhere.
433     */
434
435    vbo->draw_prims( ctx, exec->array.inputs, prim, 1, &ib, start, end );
436 }
437
438
439 static void GLAPIENTRY
440 vbo_exec_DrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices)
441 {
442    GET_CURRENT_CONTEXT(ctx);
443    GLuint min_index = 0;
444    GLuint max_index = 0;
445
446    if (!_mesa_validate_DrawElements( ctx, mode, count, type, indices ))
447       return;
448
449    if (!vbo_validate_shaders(ctx)) {
450       _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawElements(bad shader)");
451       return;
452    }
453
454    if (ctx->Array.ElementArrayBufferObj->Name) {
455       const GLvoid *map = ctx->Driver.MapBuffer(ctx,
456                                                  GL_ELEMENT_ARRAY_BUFFER_ARB,
457                                                  GL_READ_ONLY,
458                                                  ctx->Array.ElementArrayBufferObj);
459
460       get_minmax_index(count, type, ADD_POINTERS(map, indices), &min_index, &max_index);
461
462       ctx->Driver.UnmapBuffer(ctx,
463                               GL_ELEMENT_ARRAY_BUFFER_ARB,
464                               ctx->Array.ElementArrayBufferObj);
465    }
466    else {
467       get_minmax_index(count, type, indices, &min_index, &max_index);
468    }
469
470    vbo_exec_DrawRangeElements(mode, min_index, max_index, count, type, indices);
471 }
472
473
474 /***********************************************************************
475  * Initialization
476  */
477
478 void vbo_exec_array_init( struct vbo_exec_context *exec )
479 {
480 #if 1
481    exec->vtxfmt.DrawArrays = vbo_exec_DrawArrays;
482    exec->vtxfmt.DrawElements = vbo_exec_DrawElements;
483    exec->vtxfmt.DrawRangeElements = vbo_exec_DrawRangeElements;
484 #else
485    exec->vtxfmt.DrawArrays = _mesa_noop_DrawArrays;
486    exec->vtxfmt.DrawElements = _mesa_noop_DrawElements;
487    exec->vtxfmt.DrawRangeElements = _mesa_noop_DrawRangeElements;
488 #endif
489 }
490
491
492 void vbo_exec_array_destroy( struct vbo_exec_context *exec )
493 {
494    /* nothing to do */
495 }
496
497
498 /* This API entrypoint is not ordinarily used */
499 void GLAPIENTRY
500 _mesa_DrawArrays(GLenum mode, GLint first, GLsizei count)
501 {
502    vbo_exec_DrawArrays(mode, first, count);
503 }
504
505
506 /* This API entrypoint is not ordinarily used */
507 void GLAPIENTRY
508 _mesa_DrawElements(GLenum mode, GLsizei count, GLenum type,
509                    const GLvoid *indices)
510 {
511    vbo_exec_DrawElements(mode, count, type, indices);
512 }
513
514
515 /* This API entrypoint is not ordinarily used */
516 void GLAPIENTRY
517 _mesa_DrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count,
518                         GLenum type, const GLvoid *indices)
519 {
520    vbo_exec_DrawRangeElements(mode, start, end, count, type, indices);
521 }