OSDN Git Service

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