OSDN Git Service

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